Multiple left joins in laravel eloquent
11,298
Eloquent doesn't perform SQL join, but simulate it with a lot of queries.
You will get what you want doing this:
Uploads::with('commments','users')
->get(['id','comments.file_id',' comments.comment', 'users.id', 'comments.user', 'users.name']);
Using this, you need to explict have the relations in your models. Check the docs.
Another way is with Query Builder. You are NOT using Eloquent doing this, but will work, of course:
DB::table('uploads')
->select('uploads.id','comments.file_id',' comments.comment', 'users.id', 'comments.user', 'users.name')
->leftJoin('comments','uploads.id','=','comments.file_id')
->leftJoin('users','users.id','=','comments.user')
->get();
Author by
Sebastian
Updated on June 16, 2022Comments
-
Sebastian almost 2 years
I am trying to understand left joining in eloquent, but I really cant get it to work.
My regular SQL looks as following:
SELECT uploads.id, comments.file_id, comments.comment, users.id, comments.user, users.name FROM uploads LEFT JOIN comments ON uploads.id=comments.file_id LEFT JOIN users ON users.id=comments.user;
Can someone please help me to convert this to a working eloquent query? And maybe provide some info how its done?
Thank you! :D