Get all users which dont have admin role in belongsToMany relation

Posted 1 year 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-

users
- id
- name
- email

roles
- id
- name

role_user
- user_id
- role_id

Any way to do it?

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

Reply to

Use Markdown with GitHub-flavored code blocks.