First time using this awesome package, and I am trying to eager load permissions for a user.
this returns the roles in a relation:
auth()->user()->with('roles')->first();
but this returns an empty relation:
auth()->user()->with('permissions')->first();
How do I eager load permissions? The user I am using as an example is Super Admin, so it should contain an array of all permissions.
What did I miss? Thanks
Give this piece of code try
For Roles:-
use Spatie\Permission\Traits\HasRoles;
use HasRoles;
$roles = User::select('id')->with(['roles'=>function($s){
return $s->select('roles.name as r_name');
}])->first();
OR
$roles = auth()->user()->with(['roles'=>function($s){
return $s->select('roles.name as r_name');
}])->first();
For permissions :-
use Spatie\Permission\Models\Permission;
$user = Auth()->user();
$permissions = $user->getAllPermissions();
OR
$permissions = auth()->user()->getAllPermissions();