Query models that are related to one of the given models #51830
Unanswered
NiroDeveloper
asked this question in
Ideas
Replies: 1 comment 1 reply
-
Well you can write it like: $users = collect([User::create(), User::create(), User::create()])
Posts::whereHas("users", fn($q) => $q->whereIn('id', $users->pluck('id')))->get() Or even something like, and make it reusable: function relatedTo(Collection $related) {
if ($related->isEmpty()) {
return null;
}
$key = $related->first()->getKeyName();
return fn($q) => $q->whereIn($key, $related->pluck($key));
}
$users = collect([User::create(), User::create(), User::create()])
Posts::whereHas("users", relatedTo($users))->get() |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We all know that it is simple possible to receive models related to a specific model, by the relationship function.
$user->posts()->get()
For me, however, the option to search for models that are linked to one of the given models is missing in laravel.
This can be implemented like this.
I have a feature wish for a new relationship query function, to query models that are related to one of the given models.
Beta Was this translation helpful? Give feedback.
All reactions