laravel 9 roles and permissions integration example

In this section we will see how to add user roles and permissions in laravel 9 using laravel spatie permission package and laravel LaravelDaily/laravel-permission-ui Package.


Tools Use

Laravel 9

laravel spatie/laravel-permission

Laravel Breeze

LaravelDaily/laravel-permission-ui


Step 1: Install Laravel & Connect Database

Run below command to create laravel project.

composer create-project laravel/laravel role_permissions

Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password


Step 2: Install laravel spatie/laravel-permission Package

install the package via composer:

composer require spatie/laravel-permission

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

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


clear your config cache with either of these commands:

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


Add the Spatie\Permission\Traits\HasRoles trait to your User model(s):

app/Models/User.php

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

For more details you can visit. https://spatie.be/docs/laravel-permission/v5/installation-laravel


Step 3: Install Breeze

Install laravel breeze via composer:

composer require laravel/breeze --dev

Next, run below command.

php artisan breeze:install

And final install Dependencies

npm install && npm run dev 

Run migration with fresh

php artisan migrate:fresh --seed


Step 4: Install LaravelDaily/laravel-permission-ui Package

First, before installing this package, you need to have the spatie/laravel-permission installed and configured. We already done.

composer require laraveldaily/laravel-permission-ui


Go to http://localhost:8000/permissions/users and you should see a simple dashboard with three menu items on top: to manage roles, permissions and assign them to users.

That dashboard is by default protected by the auth middleware, but you can configure it, by publishing the config:

php artisan vendor:publish --provider="LaravelDaily\PermissionsUI\PermissionsUIServiceProvider" --tag="config"

For more details you can visit. https://github.com/LaravelDaily/laravel-permission-ui

Step 5: Run Laravel and vite server

php artisan serve
//and next terminal
npm run dev