Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rayan_2018's avatar

Problem with Left Join

How can i write this Join Query with Eloquent Laravel $result = DB::select("SELECT rests.* FROM rests LEFT JOIN bookings ON bookings.rest_id = rests.id Where bookings.rest_id IS NULL ");

0 likes
8 replies
Rayan_2018's avatar

Hello @Vilfago I tried your solution but not worked as you can see from my query i am trying to get unmatched records from Left Table (rests)

Vilfago's avatar

Did you try $result = App\Rest::doesntHave('bookings')->get();

Rayan_2018's avatar

no @Vilfago i used it now i got this error

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::bookings() in file C:\xampp\htdocs\weekend\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 2483

Rayan_2018's avatar
Rayan_2018
OP
Best Answer
Level 1

It worked as next $un_booked_rests = DB::table('rests')->LeftJoin('bookings','rests.id' ,'=' ,'bookings.rest_id') ->select('rests.*') ->WhereNull('bookings.rest_id') ->get();

Please or to participate in this conversation.