0

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

1
  • 2
    ->join translates to inner join Commented Nov 15, 2020 at 9:55

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

like this I am not getting all the data. only data that exist in the last table. i want to get also data that is in the first two tables but that is not exist in the last one to get null
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.