I am working with Laravel 8 and I use Laratrust package for roles and permissions
i Use the following directives to check role or permission in blade view
@permission('add_user')
<a href="route({{'add_user')}}">create new user </a>
@endpermission
@role('admin')
<a href="route({{'add_user')}}">create new user </a>
@endrole
my question is there any way to check both role and permission at the same time
some thing like
@role('admin') || @permission('add_user')
CodePudding user response:
You can check if the user has permission in the view:
$user->hasRole(['owner', 'admin']); // this returns true
Or by the logged user:
Auth::user()->hasRole('role-name');
Or this:
Auth::user()->hasPermission('permission-name');
CodePudding user response:
for this kind of job we have gate in laravel you can simply register a gate inside AuthServiceProvider.php
Gate::define('owner',function(User $user){
if($user->hasRole == "owner" && $user->hasPermission == "permission-name"){
return true;
}
return false;
})
then inside blade you can check using @can operator
@can('owner')
"write something which visible to owner"
@endcan
note don't forget to import Gate facades inside provider