Answer the question
In order to leave comments, you need to log in
Laravel pivot condition?
Users publish posts, get posts with users: Post::with('users')->get();
One post can have many users.
How can I filter already received users by condition?
For example:
foreach (...) {
$post->users->wherePivot('user_id', 1);
}
Answer the question
In order to leave comments, you need to log in
$post->users
is a collection, so either filter() or where() must be used . Well, in this case, there is no need to use pivot at all - you have a collection of users, it is much more logical to filter immediately by user id, and not by pivot.user_id.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question