2 years ago

leftJoin in Eloquent?

Posted 2 years ago by RaduCruceana

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.

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