Warm tip: This article is reproduced from serverfault.com, please click

How make a join table like bellow in laravel

发布于 2020-11-28 09:22:36

Helllo, i want to do a join between tow tables in laravel, but if table B has no rows query return null. What i want is like this.

table A

id user_id description
1    1      something
2    1       apple
3    2       cherry

table B

user_id post_id vote
(with no records initial)

result table

    id user_id description post_id  vote
    1    1      something    null   null
    2    1       apple       null   null
    3    2       cherry      null   null

How can i do that? Thank you in advance and I apologize for the worse English.

Questioner
Mihăiță Nicolaescu
Viewed
0
OMR 2020-11-28 17:30:20

you should use leftJoin

 DB::table("tableA")->leftJoin("tableB","tableA.user_id",'=','tableB.user_id')
           ->select(["tableA.id as id",'tableA.user_id as user_id','description','post_id','vote'])->get();