0

How do I create a query that joins with using() instead of on?

I have this:

$games = DB::table('games')
    ->leftJoin('game_info', 'game_id')
    ->get();

I would like it to build a query that looks like this:

select * from games left join game_info using(game_id);
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • 1
    Could maybe try `DB::table("games")->select(DB::raw("SELECT * FROM games LEFT JOIN game_info USING(game_id)"))->get();`, see if that works. – Tim Lewis Dec 31 '15 at 16:41

1 Answers1

0

It seems there is no easy way to make join this way if you don't want to create raw query. However looking at SQL documentation and after quick look at this thread using works the same as you defined in both tables same column, so query:

select * from games left join game_info using(game_id);

will work same as

select * from games left join game_info ON games.game_id = game_info.game_id

so you could simply use:

$games = DB::table('games')
    ->leftJoin('game_info', 'games.game_id','=','game_info.game_id')
    ->get();
Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291