גיא פלד
גיא פלד

Reputation: 39

Laravel multiple joins - php

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

Answers (2)

SK Shewa
SK Shewa

Reputation: 106

The following way, you can join as much as you need.

DB::table('table 1')
    ->leftJoin(....)
    ->leftJoin(....)

Note: As normal sql query:

  1. join (is inner join)
  2. leftJoin (is left join)
  3. rightJoin (is right join)

https://laravel.com/docs/6.x/queries#joins

Upvotes: 1

OMR
OMR

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

Related Questions