Ask Your Question
4

How can Laravel 9 integrate breeze and laravel-permission simultaneously?

asked 2023-05-11 00:17:11 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-11 00:20:01 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-11 00:17:11 +0000

Seen: 9 times

Last updated: May 11 '23