leftJoin in Eloquent?

Published 9 months ago by [email protected]

Hello dear friends,

I'm beginner in Laravel, I'm using 5.5 version.

Is there a method leftJoin in Eloquent ORM? I know there is one on DB facade, but if I want to use Eloquent only is there a leftJoin method?

Let's suppose, as an simple example, I have a model CarBrand and another one CarModel. CarBrand have a relationship type hasMany with CarModel, and CarModel belongsTo CarBrand.

Suppose I want to find CarBrand's id's without CarModel corespondence. I found on stackOverflow, several responses like this:

 CarBrand::leftJoin('CarModelTableName', function($join) {
      $join->on('CarBrandTableName.id', '=', 'CarModelTableName.carBrandId');
    }) ->whereNull('CarModelTableName.carBrandId')->first();

I have checked and it does function as a left join :)

But in this case is ::leftJoin an Eloquent method or silently it goes to DB facade? I can't find any for leftJoin in Laravel 5 official docs other than DB facade. Any links would be appreciated.

Hope I was clear, Thank you for your attention.

9 months ago (237,650 XP)



Thank you @jlrdw ! Do you have a little time for a little more light? :)

Best regards from Bucharest!


This mean that I can use Modell::AnyQuerryBuliderMethod ?

9 months ago (959,275 XP)

looking for a missing relation? You can use whereDoesntHave()



Thank you @Snapey !

Your answer solved my primary question!

So, If I really need a left join I will use QB, because such relationship is not implemented by Eloquent?

Sorry to bother you, but I just want to clarify things in my mind .

I'm and old fashioned SQL dinosaurus :)

9 months ago (959,275 XP)

eloquent is an orm using the query builder. All querybuider methods can be used with eloquent.

The problem you usually end up with is columns with the same name

Please sign in or create an account to participate in this conversation.