Multiple Role Base Access in Laravel 8
Building secure applications
In this article, I would like to show you how to demonstrate role base access in Laravel if you have multiple roles in your project or application. By following the below steps you can redirect users to their dashboard according to their roles.
Create A Laravel Project
You should run the below command to create a new Laravel project.
composer create-project --prefer-dist laravel/laravel RoleBaseAccess
Make sure you have already installed Laravel on your PC.
Set up your .env file
Create a new database in phpMyAdmin and enter your database name and password in the .env file.
Set up your database
Next step you should customize your database tables before migrating them. Goto the user migration file under the migrations folder add another attribute called roles.
Here I have made the role attribute nullable to avoid exception errors.
After setting up your database run the below command to migrate your database.
php artisan make:migration
Set up Authentication
Laravel provides the tools to implement authentication easily. Therefore to set up user registration and login system run the below commands.
composer require laravel/uiphp artisan ui:auth
After setting up registration and login run the below command.
php artisan serve
After running the above command, type 127.0.0.1:8000 or localhost:8000 in the browser. You can now register different users to the system through the registration link or add dummy data to the database manually.
In this project, I have used three different roles as follows.
- Admin
- Academic
- Student
Create controllers
Create controllers for each role in your project.
Run the below command to create controllers
php artisan make:controller AdminController
php artisan make:controller AcademicController
php artisan make:controller StudentController
The controller should return the blade file of each role inside the index function.
Create Middleware
Create middleware files for each role.
run:
php artisan make:middleware Adminphp artisan make:middleware Academicphp artisan make:middleware Student
Edit each middleware file. Here I have assigned each role’s middleware to its respective routes.
After editing each middleware file register them in the Kernel.php file under the route middleware array.
Create Dashboard Views
Create dashboard view files for each role as required, inside the views folder in your project.
Once you finished creating view files, set up routes for each view file inside the web.php file. (inside routes folder in your project).
Edit Login Controller
When users log in to the system you should redirect them to the correct dashboard according to the user’s role.
You can do this in the RegisterController also if required. So that user will be redirected to the dashboard based on his/her role once the user is registered to the system.
Hope this article helps to implement Laravel role base access if you have different types of roles in your system.
Thank you for reading!