Roles & Permissions dashboard Roles & Permissions dashboard
In this example, we will show how to build a simple admin panel section to manage our users' roles and permissions.
Spatie's permission package 🙏
For this, we will use Spatie's excellent package `laravel-permission`. First, let us start by installing it following Spatie's docs.
Once the package is installed with composer and the tables are migrated, we assign the `HasRoles` Trait to our User model.
Permissions table and form
Before we start, some words of wisdom from Spatie:
So we will use permissions as the base driver here, but note that if you wish to use only roles, you may simply skip this section.
The permission form
The permissions Query
Roles table and form
First we create the table Query that displays all of our application's roles along with their associated permissions.
Next, we write the Form that will insert new roles or update existing ones. Notice how we link the roles to their respective permissions by simply adding a MultiSelect component with a name of `permissions`. This name represents the `permissions()` method of the Role class that returns a BelongsToMany relationship. Behind the scenes, Kompo syncs the selected permissions with the role when the Form is submitted.
The role form
The roles Query
Users' roles & permissions
First, this is the form that allows us to modify the user's roles and below we can see a sortable table of all our app's users, their roles and permissions.