Reputation: 39
how can I left join 4 tables in laravel? This is what I am trying to do:
DB::table('table 1')
->join('table 2', 'table 1.field 1', '=', 'table 2.field 2')
->leftJoin('table 3','table 2.id','=','table 3.field 3')
->leftJoin('table 4','table 3.field 3','=','table 4.field 4')
->get();
Thanks
Upvotes: 0
Views: 50
Reputation: 106
The following way, you can join as much as you need.
DB::table('table 1')
->leftJoin(....)
->leftJoin(....)
Note: As normal sql query:
https://laravel.com/docs/6.x/queries#joins
Upvotes: 1
Reputation: 12188
just replace first join with leftJoin like the others, because normal join
will be inner join
in your sql query:
DB::table('table 1')
->leftJoin('table 2', 'table 1.field 1', '=', 'table 2.field 2')
->leftJoin('table 3','table 2.id','=','table 3.field 3')
->leftJoin('table 4','table 3.field 3','=','table 4.field 4')
->get();
Upvotes: 2