Laravel 5.1 acl roles and permission in frontend
I have followed the lesson by @jeffrey_way and tried to create ACL , but getting problems in updating the pivot table and adding permissions and roles to it.
Here is my table structure for permissions, roles, and permission_role
permission:
id name
roles:
id name
permission_role:
role_id, permission_id
Here permission_role is my pivot table.
In my frontend which i have setup something like this,
welcome.blade.php
<form method="POST" action="/give/permission">
{!! csrf_field() !!}
<select name="role" class="selectpicker" multiple data-max-options="2">
@foreach($roles as $r)
<option value="{{$r->id}}">{{$r->name}}</option>
@endforeach
</select>
<div class="checkbox">
@foreach ($perm as $p)
{!! Form::checkbox('p[]', $p->id, in_array($p->id, $all_data)) !!}
{!! Form::label('permisssion', $p->name) !!}<br>
@endforeach
</div>
<button type="submit" class="default">Submit</button>
</form>
in my controller.php
public function postrolperm(Request $request,$id){
$p = Role::find($id);
$role = $request->role;
//dd($input1);
$permission = $request->p;
//dd($input2);
//
//$role = Role::where("name", "admin")->get();
if(isset($permission) && isset($role)){
$role->givePermissionTo($permission);
$role->save();
}
return redirect::back();
}
role.php
public function givePermissionTo(Permission $permission)
{
return $this->permissions()->save($permission);
}
I am not able to save the data into the pivot table.
I have tried in php artisan tinker with following commands:
$role = Role::first(); //which gives me the first role with id of 1
$role->givePermissionTo(Permission::first()); // it will save the first permission to the role.
What i am doing wrong in my controllers ?
Also to note that, that this routes works fine.
Route::get("/pivot", function(){
$permission = new Permission;
$permission->name = "can to";
$permission->save();
$role = new Role;
$role->name = "admin2";
$role->save();
$role->givePermissionTo($permission);
return "true";
});
Please or to participate in this conversation.