2017-08-04 108 views
0

我在我的laravel项目中使用委托。 在我编辑角色时的刀片文件中,我想用复选框显示所有权限。laravel委托如何检查角色在刀片上的权限?

但我停留在思想,我想,如果角色有permission.I通过角色和所有权限刀片,并尝试

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

复选框状态被检查,但没有奏效

我也尝试将$角色和$权限转换为数组,并将它们传递给刀片并使用foreach两次,它也不起作用。 有什么办法可以做到吗?

+1

https://github.com/Zizaco/entrust#blade-templates –

+0

我尝试@权限,但它只是审查登录用户的权限,而不是我想编辑的角色。 –

回答

0

你可以试试这个:

@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 
+0

谢谢这是一个比我的更好的方式 –

+0

@EvolRof很高兴帮助和我的答案是你的解决方案,然后请upvote我的答案。 –

0

原来,$角色也可以调用调用hasPermission方法

@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