Role And Permission In Laravel 11 using Spatie

by Tarun Korat Jun 25, 2024 Total Views: 85

Laravel Version Compatibility


PACKAGE VERSION ==> LARAVEL VERSION


^6.0 ==> 8,9,10,11 (PHP 8.0+)

^5.8 ==> 7,8,9,10

^5.7 ==> 7,8,9

^5.4-^5.6 ==> 7,8

5.0-5.3 ==> 6,7,8

^4 ==> 6,7,8

^3 ==> 5.8


Roles and permissions can make or break your web application. If you’re not careful, your users might end up running amok, wreaking havoc on your carefully crafted code. Roles and permissions are like the bouncers at a club. They make sure only the right people get in and keep the troublemakers out. Without them, chaos would ensue, and you’d have a bunch of unruly patrons messing things up. In the same way, managing user roles and permissions in Laravel is essential to keep your application secure and prevent unauthorized access.


A permission is the right to have access to something, such as a page in a web app. A role is just a collection of permissions.


Installing

  1. Consult the Prerequisites page for important considerations regarding your User models!
  2. This package publishes a config/permission.php file. If you already have a file by that name, you must rename or remove it.
  3. You can install the package via composer:


 composer require spatie/laravel-permission


Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:


'providers' => [
    // ...
    Spatie\Permission\PermissionServiceProvider::class,
];


You should publish the migration and the config/permission.php config file with:


php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"


BEFORE RUNNING MIGRATIONS:


  • f you are using UUIDs, see the Advanced section of the docs on UUID steps, before you continue. It explains some changes you may want to make to the migrations and config file before continuing. It also mentions important considerations after extending this package's models for UUID capability.
  • If you are going to use the TEAMS features you must update your config/permission.php config file:
  • must set 'teams' => true,
  • and (optional) you may set team_foreign_key name in the config file if you want to use a custom foreign key in your database for teams
  • If you are using MySQL 8, look at the migration files for notes about MySQL 8 to set/limit the index key length, and edit accordingly. If you get ERROR: 1071 Specified key was too long then you need to do this.


Clear your config cache. This package requires access to the permission config settings in order to run migrations. If you've been caching configurations locally, clear your config cache with either of these commands:


 php artisan optimize:clear
 # or
 php artisan config:clear


Run the migrations: After the config and migration have been published and configured, you can create the tables for this package by running:


 php artisan migrate


Add the necessary trait to your User model:


 // The User model requires this trait
 use HasRoles;


  1. Consult the Basic Usage section of the docs to get started using the features of this package.


Tags: Permissions Laravel 11 Role and Permission Spatie Permission augmented reality framework Laravel PHP

Please login to create comment.

Author
Author Image

"Tarun Korat, an avid wordsmith and passionate storyteller, pours his creativity into every piece he writes. With a knack for weaving engaging narratives, Tarun captivates readers with his insightful perspectives on life, love, and everything in between. Join him on his literary journey as he shares his thoughts and musings through the power of words."

- Tarun Korat
Follow Me: