We now need to repeat the process we went through in the last post for the permission table. Let's start with scaffolding the permissions table
php artisan infyom:scaffold permissions --fromTable --tableName=permissions
Next remove the gaurd_name from \resources\views\permissions\show_fields.blade.php and from \resources\views\permissions\fields.blade.php
Now you can go ahead and create new permissions. Permissions could include such functions as Create New Member, Delete Member, Delete Booking.
Next, we need to make changes to the rolesController.php to add the functions which allow permissions to be assigned. The assignPermissions function
public function assignPermissions($id) { $role = SpRole::findOrFail($id); $permissions = SpPermission::all(); return view('roles.assignpermissions') ->with('role', $role)->with('permissions',$permissions); }
And the updatePermissions function
public function updatePermissions($id, Request $request)
{
$role = SpRole::findOrFail($id);;
$permissions = SpPermission::all();
foreach($permissions as $permission) {
if (isset($request->permission[$permission->id])) {
$role->givePermissionTo($permission);
}
else {
$role->revokePermissionTo($permission);
}
}
Flash::success('Roles updated successfully.');
return redirect(route('roles.index'));
}
To use the Spatie Role and Spatie Permissions classes we must add the use clauses to the top of the class as follows
use Spatie\Permission\Models\Role as SpRole;
use Spatie\Permission\Models\Permission as SpPermission;
Now we need a view to allow us to click checkboxes for each of the Permissions we want to assign to the Role. Save the following file as \resources\views\roles\assignpermissions.blade.php
@extends('layouts.app') @section('content') <style> .control-label { text-align: right; } </style> <section class="content-header"> <h1> Permissions for Role: <b>{{$role->name}}</b> </h1> </section> <div class="content"> <div class="box box-primary"> <div class="box-body"> {!! Form::model($role, ['route' => ['roles.permissionsupdate', $role->id], 'method' => 'patch']) !!} <div class="row" style="padding-left: 20px"> <div class="form-group col-sm-4"> @foreach($permissions as $permission) <label class="control-label col-sm-10">{{$permission->name}}</label> <div class="col-sm-2"><input class="checkbox-inline" type="checkbox" name="permission[{{$permission->id}}]" @if($role->getAllPermissions()->contains($permission)) checked @endif ></div> @endforeach </div> <div class="form-group col-sm-12"> {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} <a href="{!! route('roles.index') !!}" class="btn btn-default">Cancel</a> </div> </div> {!! Form::close() !!} </div> </div> </div> @endsection
Finally to make the system work you need to add appropriate routes. Place the following lines in routes\web.php
Route::get('/roles/assignpermissions/{id}', 'App\Http\Controllers\RolesController@assignPermissions')->name('roles.assignpermissions'); Route::patch('/roles/updatepermissions/{id}', 'App\Http\Controllers\RolesController@updatePermissions')->name("roles.permissionsupdate");
The routes appear in the file as follows
Having created permissions such as Create New Member, Delete Member and Delete Booking, now if you visit http://localhost:8000/roles/assignpermissions/3, where 3 is the id of the Club Secretary role, I get the following screen