jeudi 21 septembre 2017

Laravel 5.1 constrain and eager load multiple nested relations

I have four models: OwnerCompany, Owner, User, and Role.

I want to get all OwnerCompanys eager loading their Owner and also eager loading its Users who have the Role with the name 'admin'.

OwnerCompany::with('owner.users')->whereHas('owner.users.roles', function ($query) {
    $query->where('name', 'admin');
})->get();

This loads in the models but doesn't constrain the users, they are all loaded.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire