Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

How to Create Multiple Role Based Authentication and Access control in Laravel 5.8

LaravelWebsites

 

1st step:-open command prompt on xampp\htdocs directory.

2nd step:-Create laravel project with command given below

3rd step:- move to the project directory on command prompt.

4th step:-Write the command given below for user      Authentication

     php artisan make:auth

5th step:- write the given code in  (database/migration/2014_10_12_000000_create_user_table.php)

6th step:- create model and migration. Write the command given below in command prompt.

           php artisan make:model Role -m

7th step:- write the given code in  (database/migration/ 2020_06_10_060108_create_roles_table)

8th step:- In role model create users() function for relationship. Paste the code in Role.php file given below:-

9th step:- In user model create roles() function for relationship.

Paste the code in User.php file given below:-

10th step:- Now create seeder file for user. File name                UserTableSeeder. Write the command given below in command prompt to create UserTableSeeder:-

php artisan make:seed UsersTableSeeder

   11th step:- Now create seeder file for Role. File name                RoleTableSeeder. Write the command given below in command prompt to create RoleTableSeeder:-

        php artisan make:seed RolesTableSeeder

12th step:-Add insert function in RolesTableSeeder.php file(database/seeds/RolesTableSeeder.php)

Add DB file given below:-

use Illuminate\Support\Facades\DB;

Paste the code given below in RolesTableSeeder.php:-

13th step:-Add insert function in RolesTableSeeder.php file(database/seeds/UsersTableSeeder.php).

Add DB file given below:-

use Illuminate\Support\Facades\DB;

Paste the code given below in UsersTableSeeder.php:-

14th step:-Create Database in mysql.(database name=devopsschool)

15th step:- Go to .env file and set database=devopsschool,username=root

Code for .env file is given below:-

16th step:- Migrate the table to the database. Write the command given below in command prompt:-

php artisan migration

17th step:- Define RoletableSeeder and UserTableSeeder class on Database.php file. The code is given below:-

18th step:- insert datas in tables. Write the code given below in command prompt:

   php artisan db:seed

19th step:- create DashboardController for Admin. Write the code given below in command prompt:

php artisan make:controller Admin/DashboardController

20th step:- create DashboardController for Admin. Write the code given below in command prompt:

php artisan make:controller User/DashboardController

21st step:- create Adminmiddleware for Admin. Write the code given below in command prompt.

php artisan make:middleware AdminMiddleware

22nd step:- create Usermiddleware for User. Write the code given below in command prompt.

php artisan make:middleware UserMiddleware

23rd step:- Use Auth class for authentication in AdminMiddleware.php

24th step:- implement condition in handle() function of AdminMiddleware.php. Code of AdminMiddleware.php is given below:

25th step:- Use Auth class for authentication in UserMiddleware.php.

26th step:- :- Implement condition in handle() function of UserMiddleware.php. Code of UserMiddleware.php is given below:

27th step:-   Implement Condition for Admin and User Login in RedirectIfAuthenticated(app/Http/Middleware/RedirectIfAuthenticated.php). code given below:

28th step:- remove =/home from protected $redirectTo = ‘/home’; from (app/Http/Controllers/Auth/LoginController.php).

29th step:- implement condition in _construct() function in (app/Http/Controllers/Auth/LoginController.php).

 code given below:-

30th step:- remove =/home from protected $redirectTo = ‘/home’; from (app/Http/Controllers/Auth/LoginController.php).

31st step:- implement condition in _construct() function in (app/Http/Controllers/Auth/ResetPasswordController.php)

code given below:-

32nd step:- use AdminMiddleware in Kernal.php file

use App\Http\Middleware\AdminMiddleware;

33rd step:-Define AdminMiddleware in routemiddleware in kernel.php file

'admin' => AdminMiddleware::class,

34th step:- use UserMiddleware in Kernal.php file

use App\Http\Middleware\UserMiddleware;

35th step:- define UserMiddleware in routeMiddleware

'user' => UserMiddleware::class,

36th step:- set Admin middleware and Route. In (route/web.php)

37th step:- set user middleware and route. In (route/web.php)

Code of web.php file is given below:-

shashank K
Latest posts by shashank K (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x