1 | initial version |
To integrate Laravel 9 Breeze and Laravel Permission, you can follow the below steps:
Step 1: Install Laravel Breeze and Laravel Permission
You can install Laravel 9 Breeze and Laravel Permission using composer:
composer require laravel/breeze
composer require spatie/laravel-permission
Step 2: Generate Breeze scaffolding
To generate Laravel Breeze scaffolding, use the following command:
php artisan breeze:install
This command will install Breeze, generate the auth scaffolding, and make some changes to your routes, views, and configurations.
Step 3: Generate Permission scaffolding
To generate Laravel Permission scaffolding, use the following command:
php artisan make:auth
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
The above commands will create the authentication system and migrate the database tables for Laravel Permission.
Step 4: Combine Breeze and Permission routes
To combine Laravel Breeze and Permission routes, you need to make changes to your web.php
file.
Change Auth::routes();
in the web.php
file to:
require __DIR__.'/auth.php';
Then, add the following code to the beginning of auth.php
:
<?php
use Illuminate\Support\Facades\Route;
Route::group(['middleware' => 'web'], function () {
Route::auth();
Auth::routes();
});
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
});
Step 5: Update the User model
To use Laravel Permission, you need to update the User model. Add the following to the User model:
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasFactory, Notifiable, HasRoles;
}
Step 6: Add middleware
Finally, add middleware to your routes to protect them from unauthorized access. Use the following code to protect your routes:
Route::group(['middleware' => ['auth', 'role:admin']], function () {
// Your protected routes here
});
That's it! You have successfully integrated Laravel Breeze and Laravel Permission in Laravel 9.