mardi 9 mai 2017

Laravel Eloquent - How Do You Add a Where Condition to a Distantly Related Model Using the ORM?

I currently have the following:

Cars::with('cases')->with(['parts', 'parts.engines', 'parts.engines.metals'])
                   ->orderBy('car_name', 'DESC')->orderBy('id', 'DESC');

The above will list all rows in my cars table along with the metal associated with the engine in each of those cars. The metals table is related to the cars table through the parts and then the engines tables.

I've tried using:

Cars::with('cases')->whereHas(['parts', 'parts.engines', 'parts.engines.metals'], function($query){
    $query->where('weight', '=', 45)
})->orderBy('car_name', 'DESC')->orderBy('id', 'DESC');

But this errors out since whereHas() does not accept an array for its first parameter and I don't see a way to link to distant relationships with it.

How do I apply a WHERE conditional on a column in the metals table using the built-in ORM?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire