简体   繁体   中英

Laravel 5.1 acl roles and permission in frontend

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";

});

Did you added

use App\Role;

in controller.php file?

The reason it isn't working is because you haven't created a Role and Permission object based on the id s passed back from the view. A working example is given below:

$role_id = $request->role;
$permission_ids = $request->p; // This comes back as an array.
if(isset($permission_ids) && isset($role_id)){
    $role_object = Role::where('id', $role_id)->firstOrFail();
    foreach ($permission_ids as $permission_id) {
        // loop through each permission id
        // find the corresponding permission object in the database
        $permission_object = Permission::where('id', $permission_id)->firstOrFail();

        // assign the object to the role
        $role_object->givePermissionTo($permission_object);
    }
    $role_object->save();
}
return Redirect::back()

Note that this will throw an exception if the role and permission passed back from the view don't correspond to a valid id in the database. Use first() instead of firstOrFail() if you want failures to be silent, ie not thrown an exception.

Make sure to add use App\\Role; and use App\\Permission to the top of your Controller file.

Your version fails due to the fact that you are trying to call the givePermissionTo function on a string representing the role id passed from the view - $role . You missed the step in between of getting the Role object from the database.

Curiously, you were on the right track with your $p variable, however you also needed to grab the corresponding $permission object from the database as shown in my example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM