2 years ago

Get all users which dont have admin role in belongsToMany relation

Posted 2 years ago by saqueib

I want to get all user which don't have admin role, it's a belongsToMany relation where a user can have many roles. I want to get only the user who is not an admin.

I have tried following but it gives me users nested in each role, I want it a list of users limit(5) as a collection.

return Role::with('users')->where('name', '!=', 'admin')->get();

here is table structure-

- id
- name
- email

- id
- name

- user_id
- role_id

Any way to do it?

Please sign in or create an account to participate in this conversation.