I
I
Ilya Parshakov2018-04-23 13:08:02
Laravel
Ilya Parshakov, 2018-04-23 13:08:02

How to get all articles by author role?

Hello!
The Post and User models are in a one-to-many relationship.
There are Roles: admin, user, manager (laratrust).
How can I get all Post entries by author role like user and manager?
Something like:

Post::whereHas('user', function($query) {
                $query->hasRole(['user', 'manager']);
            })
            ->paginate(10);

This code results in an error:
"Call to undefined method Illuminate\Database\Query\Builder::hasRole()"

Thank you for your attention! :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Al Sm, 2018-04-23
@parschakov

Post::whereHas('user', function($query) {
                $query->whereRoleIs(['user', 'manager']);
            })
            ->paginate(10);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question