Search code examples
laravelentrust

laravel entrust how to check a role's permission in blade?


I am using entrust in my laravel project. In blade file when I edit a role I want to display all the permissions with checkbox.

But I stuck at the thought that I want the checkbox status is checked if the role has the permission.I pass the role and all permissions to blade ,and try

@foreach($permissions as $permission)
  <input type="checkbox" value="{{$permission->name}}"
    @if($role->hasPermission($permission->name))
    checked="checked"
    @endif
@endforeach

but it didn't work

I also try convert $role and $permissions to array and pass them to blade and use foreach twice ,it didn't work either. Is there any way to make it?


Solution

  • You can try this:

    @foreach($permissions as $permission)
        @foreach($roles as $role)
            @if($permission->hasRole($role->name))
                <input type="checkbox" checked="checked" name="perms[[]" value="{{ $permission->id }}">
            @else
                <input type="checkbox" name="perms[]" value="{{ $permission->id }}">
            @endif
        @endforeach
    @endforeach