mardi 17 mai 2016

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=""></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($id){
    $p  = Role::find($id);
    $role = $request->role;

    $permission = $request->p;


  //$role = Role::where("name", "admin")->get();

  if(isset($permission) && isset($role)){
    $role->givePermissionTo($permission);
  }
  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 ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire