noblemfd
1 month ago
233
2
Laravel

User Roles and Permissions (acl) using Spatie Example Laravel 5.8

Posted 1 month ago by noblemfd

I found this package very useful, but I need some help:

User Roles and Permissions (acl) using Spatie Example Laravel 5.8 https://github.com/savanihd/user-roles-and-permissions-acl-using-spatie-tutorial-laravel-5.8:

On the create new role where roles are created and permissions added

class RoleController extends Controller
{
function __construct()
{
     $this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index','store']]);
     $this->middleware('permission:role-create', ['only' => ['create','store']]);
     $this->middleware('permission:role-edit', ['only' => ['edit','update']]);
     $this->middleware('permission:role-delete', ['only' => ['destroy']]);
}

public function create()
{
    $permission = Permission::get();
    return view('roles.create',compact('permission'));
}

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|unique:roles,name',
        'permission' => 'required',
    ]);
    $role = Role::create(['name' => $request->input('name')]);
    $role->syncPermissions($request->input('permission'));
    return redirect()->route('roles.index')
                    ->with('success','Role created successfully');
}

public function edit($id)
{
    $role = Role::find($id);
    $permission = Permission::get();
    $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)
        ->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id')
        ->all();
    return view('roles.edit',compact('role','permission','rolePermissions'));
}

public function update(Request $request, $id)
{
    $this->validate($request, [
        'name' => 'required',
        'permission' => 'required',
    ]);
    $role = Role::find($id);
    $role->name = $request->input('name');
    $role->save();
    $role->syncPermissions($request->input('permission'));
    return redirect()->route('roles.index')
                    ->with('success','Role updated successfully');
}
}

view: roles: create,blade

{!! Form::open(array('route' => 'roles.store','method'=>'POST')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
        <strong>Name:</strong>
        {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
    </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
        <strong>Permission:</strong>
        <br/>
        @foreach($permission as $value)
            <label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }}
            {{ $value->name }}</label>
        <br/>
        @endforeach
    </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
    <button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}

I have these issues:

  1. How do I add SelectAll/UnselectAll to the Role Permissions checkboxes

  2. The checkboxes for permission under roles are in one column. It will be too clumpsy if they are many. How can I div it to there (3) columns to make it neater.

  3. Is it possible to display the permissions under groups based on the Controller name like Product, Sales ...

Please sign in or create an account to participate in this conversation.