Laravel Roles – Role-based permission handling

In Laravel, handling role-based permissions can be managed using packages like Spatie’s Laravel Permission package, which simplifies adding roles and permissions to your application.

Step 1: Install Spatie Laravel Permission Package

First, install the Spatie Laravel Permission package via Composer:

bash

Step 2: Publish the Configuration File

After installing the package, publish its configuration and migration files using the following command:

bash

This will create a configuration file at config/permission.php and migration files for roles and permissions tables.

Step 3: Run Migrations

To create the required tables in your database, run the migrations:

bash

This will create the following tables:

  • roles
  • permissions
  • model_has_roles
  • model_has_permissions
  • role_has_permissions

Step 4: Add the HasRoles Trait to Your User Model

Next, add the HasRoles trait to your User model to enable role and permission handling for users.

In app/Models/User.php, import the HasRoles trait and use it:

php
 

Step 5: Create Roles and Permissions

You can now create roles and permissions in your Seeder or directly in your code.

Example: Seeder for Roles and Permissions

Create a seeder file to define roles and permissions. Run this command:

bash

In database/seeders/RolePermissionSeeder.php, define roles and permissions:

php

Run the seeder to insert roles and permissions into the database:

bash
 

Step 6: Assign Roles and Permissions to Users

Now you can assign roles and permissions to users in your application.

Example: Assign Role to User

php

Example: Assign Permission to a User

php
 

Step 7: Checking Roles and Permissions

Now you can check if a user has a certain role or permission using the following methods:

Check Role

php

Check Permission

php
 

Step 8: Middleware for Role and Permission

To restrict access to routes or controllers based on roles or permissions, you can use middleware provided by the package.

Example: Role Middleware in Routes

In your routes/web.php, use the role middleware:

php

Example: Permission Middleware in Routes

Use the permission middleware to check permissions:

php
 

Step 9: Middleware Configuration (Optional)

If you want to create your own middleware for roles and permissions, you can add custom middleware to app/Http/Kernel.php:

php

Now you can apply these middlewares on routes or controllers to handle access control.


Summary

You’ve successfully set up role-based permission handling using Spatie’s Laravel Permission package. Here’s a quick recap:

  1. Install the package using Composer.
  2. Publish configuration and run migrations.
  3. Use the HasRoles trait in the User model.
  4. Create roles and permissions using a seeder or in code.
  5. Assign roles and permissions to users.
  6. Check roles/permissions in your application.
  7. Apply middleware for restricting access based on roles or permissions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top