Laravel 8 User Roles and Permissions Tutorial (ok)
https://www.itsolutionstuff.com/post/laravel-8-user-roles-and-permissions-tutorialexample.html
Một ví dụ đã hoàn thành
C:\xampp\htdocs\test\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProductController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home')->middleware('verified');
Route::group(['middleware' => ['auth']], function() {
Route::resource('roles', RoleController::class);
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
});C:\xampp\htdocs\test\app\Models\Role.php
C:\xampp\htdocs\test\database\seeders\RoleSeeder.php
C:\xampp\htdocs\test\database\factories\RoleFactory.php
Source đã hoành chỉnh 👍👍👍👍)
Today our leading topic is laravel 8 roles and permissions tutorial. In this article, we will implement a laravel 8 spatie user roles and permissions tutorial. i explained simply step by step laravel 8 spatie/laravel-permission. step by step explain laravel 8 acl tutorial.
we are using spatie github package for roles and permissions in laravel 8 application. just follow bellow step to create acl in laravel 8.
Spatie role permission composer package provide way to create acl in laravel 8. they provide how to assign role to user, how to assign permission to user and how to assign permission assign to roles. i will write step by step creating roles and permissions in laravel 8 application.
I also posted on tutorial for ACL User Roles and Permissions using entrust package, you can see here : Laravel - User Roles and Permissions (ACL) using entrust package.
Roles and Permissions through you can create several types of users with different role and permission, i mean some user have only see listing of items module, some user can also edit items modules, for delete and etc.
In this examples i created three modules as listed bellow:
User Management
Role Management
Product Management
After register user, you don't have any roles, so you can edit your details and assign admin role to you from User Management. After that you can create your own role with permission like role-list, role-create, role-edit, role-delete, product-list, product-create, product-edit, product-delete. you can check with assign new user and check that.
You need to just follow few step and you will get full example of ACL:

List Role:

Create Role:

Create User:

List Product:

Step 1: Laravel 8 Installation
We are going from scratch so, If you haven't installed laravel in your system then you can run bellow command and get fresh Laravel project.
Step 2: Install Composer Packages
Now we require to install Spatie package for ACL, that way we can use it's method. Also we will install form collection package. So Open your terminal and run bellow command.
Now open config/app.php file and add service provider and aliase.
config/app.php
We can also custom changes on Spatie package, so if you also want to changes then you can fire bellow command and get config file in config/permission.php and migration files.
Now you can see permission.php file and one migrations. so you can run migration using following command:
Read Also: Laravel 8 CRUD Application Tutorial for Beginners
Step 3: Create Product Migration
In this step we have to create three migration for products table as using bellow command:
products table:
Now run migration:
Step 4: Create Models
In this step we have to create model for User and Product table, so if you get fresh project then you have User Model have so just replace code and other you should create.
app/Models/User.php
app/Models/Product.php
Step 5: Add Middleware
Spatie package provide it's in-built middleware that way we can use it simply and that is display as bellow:
role
permission
So, we have to add middleware in Kernel.php file this way :
app/Http/Kernel.php
Step 6: Create Authentication
You have to follow few step to make auth in your laravel 8 application.
First you need to install laravel/ui package as like bellow:
Here, we need to generate auth scaffolding in laravel 8 using laravel ui command. so, let's generate it by bellow command:
Now you need to run npm command, otherwise you can not see better layout of login and register page.
Install NPM:
Run NPM:
Step 7: Create Routes
We require to add number of route for users module, products module and roles module. In this this route i also use middleware with permission for roles and products route, so add route this way:
routes/web.php
Step 8: Add Controllers
In this step we have add three controller for users module, products module and roles module so you can create three controller like as bellow:
app/Http/Controllers/UserController.php
app/Http/Controllers/ProductController.php
app/Http/Controllers/RoleController.php
Step 9: Add Blade Files
In this step, we need to create following files as like listed bellow:
Theme Layout
app.blade.php
Users Module
index.blade.php create.blade.php edit.blade.php show.blade.php
Roles Module
index.blade.php create.blade.php edit.blade.php show.blade.php
Product Module
index.blade.php create.blade.php edit.blade.php show.blade.php
So, let's create following files:
resources/views/layouts/app.blade.php
resources/views/users/index.blade.php
resources/views/users/create.blade.php
resources/views/users/edit.blade.php
resources/views/users/show.blade.php
resources/views/roles/index.blade.php
resources/views/roles/create.blade.php
resources/views/roles/edit.blade.php
resources/views/roles/show.blade.php
resources/views/products/index.blade.php
resources/views/products/create.blade.php
resources/views/products/edit.blade.php
resources/views/products/show.blade.php
Step 10: Create Seeder For Permissions and AdminUser
In this step we will create seeder for permissions, Right now we have fixed permission so we create using seeder as listed bellow, but if you can add more permission as you want:
1.role-list
2.role-create
3.role-edit
4.role-delete
5.product-list
6.product-create
7.product-edit
8.product-delete
So, first create seeder using bellow command:
And put bellow code in PermissionTableSeeder seeder this way:
database/seeds/PermissionTableSeeder.php
After this we have to run bellow command for run PermissionTableSeeder seeder:
Now let's create new seeder for creating admin user.
database/seeds/CreateAdminUserSeeder.php
Now we are ready to to run full example of ACL. so let's run our example so run bellow command for quick run:
Access By
Now you can login with following credential:
Read Also: Laravel 8 Stripe Payment Gateway Integration Example
Now you can run and check.
You can also download code from git: Download Code from Github
I hope it can help you....
Last updated
Was this helpful?