0

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?

Evol Rof
  • 2,528
  • 2
  • 22
  • 37

2 Answers2

1

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
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

turns out that $role also can call the hasPermission method

 @foreach($permissions as $permission)
   <div class="checkbox pull-left" >
        <label class="">
            <input type="checkbox" name="perms[]" value="{{$permission->id}}" 
             @if($role->hasPermission($permission->name))  checked @endif>

          <p>{{$permission->display_name}}</p>
        </label>
   </div>
 @endforeach
Evol Rof
  • 2,528
  • 2
  • 22
  • 37