Laravel-permission: How to retrieve users without any assigned role?

Created on 25 Feb 2020  路  2Comments  路  Source: spatie/laravel-permission

How to retrieve users without any assigned role?

support

Most helpful comment

User::doesntHave('roles')->get();

All 2 comments

You can leverage the Eloquent relations to find the users without the relationship by querying the count of those who "do" have the relationship, where the count is 0:

use \App\User;
use \Spatie\Permission\Models\Role;
$usersWithoutRoles = User::withCount('roles')->has('roles', 0)->get();

User::doesntHave('roles')->get();

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vpratfr picture vpratfr  路  4Comments

holymp2006 picture holymp2006  路  4Comments

feliperoan picture feliperoan  路  3Comments

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments

ergonomicus picture ergonomicus  路  3Comments