vendredi 3 mai 2019

Does a where for belongsTo relations exist in laravel?

Example

Role -> hasMany -> User

If I want to get all users with a specific role I would normally do something like this:

$role = Role::findOrFail(1);
User::where('role.id', $role->id)->get()

or

User::where('role_id', $role->id)->get()

My question is: Is there any way to achieve that without using role_id or id. I want to just use the $role object. Something like this would be great:

User::where('role', $role)->get();

I know I can use $role->users, but I leaving this out, because I cannot use that if I have multiple belongsTo relations that I want to query.

Example

$model->where('otherModel', $otherModel1)->where('differentModel', $differentModel1)->get();

Background

A solution like this would help a lot, if you want to rename a primary or a foreign key or if your application has keys with counterintuitive names.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2V1wslX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire