Friday, March 17, 2017

Laravel Entrust Role & Permissions

Hello i want to be able to assign permissions to roles from my admin panel i am getting the permissions foreach role but i canot save them see the Photo i want now to check the boxes and to sync the checked boxes with the database so the permissions to be saved for the roles This is my view:

 {!! Form::open(['route' => 'permission.save']) !!}

    <div class="content table-responsive table-full-width">
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    @foreach($roles as $role)
                        <th></th>
                    @endforeach
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody id="tables">
                @if(count($permissions))
                    @foreach($permissions as $permission)
                        <tr>
                            <td></td>
                            @foreach($roles as $role)
                                <td>
                                    {!! Form::checkbox("roles[{$role->id}][]", $permission->id, $role->hasPermission($permission->name)) !!}
                                </td>
                            @endforeach
                            <td class="td-actions ">
                            <a style="height: 40px; width: 40px;" class=" remove btn btn-icon btn-danger">
                                <i style="margin-top: 3px;" class="fa fa-remove"></i>
                            </a>

                        </td>
                        </tr>
                    @endforeach
                @endif
            </tbody>
        </table>

        <button type="submit" class="btn btn-primary">Save</button>
    </div>
    {!! Form::close() !!}

Here is my controller

    public function saveRolePermissions(Request $request)
{
    $roles = $request->get('roles');
    $allRoles = $this->roles->pluck('id');
    // dd($allRoles);
    foreach ($allRoles as $role) {

        $permissions = array_get($roles, $role, []);
        // if (!$role->perms()->get()->contains('id', $permission->id)) {
            $role->attachPermission($permission);
        // }

    }
}



via Eth0

Advertisement