W
W
WebDev2016-06-23 13:40:12
Laravel
WebDev, 2016-06-23 13:40:12

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);
}

This entry does not work. Pivot comes to $post->users. How to filter on it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-06-23
@kirill-93

$post->usersis 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 question

Ask a Question

731 491 924 answers to any question