I am using Zizaco / entrust in Laravel 5.1. I want to explicitly grant permissions for a specific role using checkboxes. This is my code:
Extraction of roles:
fetchRoles: function(){ this.$http.get('api/role',function(data){ this.$set('roles',data); }); }
Obtaining access rights:
fetchPermissions: function(){ this.$http.get('api/permission',function(data){ this.$set('permissions',data); }); }
Here is a table for assigning roles and permissions:
<table class="table table-bordered table-responsive"> <thead> <th>Permissions/Roles</th> <th v-for="role in roles"> @{{ role.display_name }} </th> </thead> <tbody> <tr v-for="permission in permissions"> <td>@{{ permission.display_name }}</td> <td v-for="role in roles"> <input type="checkbox" value="@{{ permission.id }}" name="roles[@{{ role.id }}][permissions][]"> </td> </tr> </tbody> <tfoot> <tr> <td> <button type="submit" class="btn btn-primary">Alter Permission</button> </td> </tr> </tfoot> </table>
The output is as follows: Screenshot of the results
Permissions are also successfully assigned in the following way:
public function changePermission(Request $request){ $input = $request->all(); $roles = Role::all(); foreach($roles as $role) { $permissions_sync = isset($input['roles'][$role->id])? $input['roles'][$role->id]['permissions'] : []; $role->perms()->sync($permissions_sync); } Flash::success('Successfully modified permissions!'); return redirect()->back(); }
The only thing I'm stuck with is to make the checkboxes checked if the role has a specific permission.
I could do the following if it was in a php object:
@if(count($permissions)>0) @foreach($permissions as $permission) <tr> <td>{{$permission->display_name}}</td> @if(count($roles)>0) @foreach($roles as $role) <td><input type="checkbox" value="{{$permission->id}}" name="roles[{{$role->id}}][permissions][]" @if($role->hasPermission($permission->name)) checked="checked" @endif></td>@endforeach @endif </tr> @endforeach @endif
How can I use the hasPermission () method in Vue JS?
source share