leftJoin in Eloquent?

Published 3 weeks 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.

jlrdw
jlrdw
3 weeks ago (200,000 XP)

QB

raducruceana@yahoo.com

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

Best regards from Bucharest!

raducruceana@yahoo.com

This mean that I can use Modell::AnyQuerryBuliderMethod ?

Snapey
Snapey
3 weeks ago (705,395 XP)

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

https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-absence

raducruceana@yahoo.com

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 :)

Snapey
Snapey
3 weeks ago (705,395 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.