Introduction
In this tutorial, you will learn how to add Role & Permission Management to your Laravel 12 CRUD app using the Spatie Laravel Permission package.
This allows you to assign roles (like Admin, Editor, User) and control what each role can do — such as creating, editing, or deleting posts.
Requirements
Before we start, make sure you have:
-
A working Laravel 12 CRUD application with user authentication.
-
Composer is installed on your system.
-
Basic knowledge of Laravel routes, controllers, and Blade views.
Step 1 — Install the Spatie Permission Package
Open your terminal and run this command to install the package via Composer:
Step 2 — Publish the Configuration and Migration Files
Publish the package’s config file and database migrations:
This will create:
-
A config file:
config/permission.php -
Migration files for roles, permissions, and pivot tables.
Step 3 — Run the Migrations
Run the migrations to create the necessary tables:
You should now see new tables: roles, permissions, model_has_roles, model_has_permissions, and role_has_permissions.
Step 4 — Add the HasRoles Trait to Your User Model
Open app/Models/User.php and add the following at the top of the class:
Then inside the User class, add the trait:
This enables role and permission management on your User model.
Step 5 — Create Initial Roles and Permissions
Create a seeder to add some default roles and permissions:
Edit the seeder file database/seeders/RolesAndPermissionsSeeder.php like this:
Run the seeder:
Step 6 — Register Middleware
In Laravel 12, route middleware aliases are no longer registered inside app/Http/Kernel.php.
Instead, you add them in the bootstrap/app.php file.
Open bootstrap/app.php and look for this section (near the bottom):
Now replace it (or extend it) with the following code to register Spatie’s middleware:
Step 7 — Assign Roles to Users
You can assign roles manually in tinker or add role assignment in your registration logic.
Using tinker:
Step 8 — Protect Routes Using Middleware
In your routes/web.php, protect routes by role or permission.
Example: Only allow users with the admin role to access certain routes:
Or allow users with the create posts permission to access create route:
Step 9 — Use Blade Directives to Show/Hide UI Elements
You can use Blade directives in your views to display buttons or links only to users with certain roles or permissions.
Example, in resources/views/posts/index.blade.php:
Step 10 — Add Permission Checks in Your Controller
In PostController.php, use middleware and authorization to protect actions:
Step 11 — Test Your Role & Permission Setup
-
Create users and assign them different roles.
-
Log in as each user and test if they can perform only allowed actions.
-
Check if unauthorized users are blocked or redirected properly.
Conclusion
You’ve successfully added Role & Permission Management to your Laravel 12 CRUD app using the Spatie Laravel Permission package.
This lets you build scalable apps with fine-grained access control.

