How to Create Multiple User Based Access Control in Laravel

Step 1: first go to xampp to htdocs and open command prompt or Git Bash and type command to create project.

composer create-project --prefer-dist laravel/laravel devopsschool "5.8.*"

Step 2: Goto project and right click and open vs code and open terminal.

Step 3: For User Authentication write this command in terminal

php artisan make:auth

Step 4:  customize users table(database/migration/2014_10_12_000000_create_users_table.php)

and added below code

Step 5: Create Model and Migration , controller and routes for Role Table

php artisan make:model Role -mcr

Step 6: customize roles table(database/migration/2020_06_10_060108_create_roles_table)

add now

Step 7: Make Relationship between users and roles tables through Role Model and User Model.

Step 7(A): In Role Model Create users() function for relationship.

Step 7(B): In User Model Create roles() function for relationship

Step 8: To  Create UsersTableSeeder file for insert data in users table through migration follow command

tpye in tarminal

php artisan make:seed UsersTableSeeder

Stp 9: To Create RolesTableSeeder file for insert data in users table through migration follow this command.

php artisan make:seed RolesTableSeeder

Step 10: Add Insert function in RolesTableSeeder.php(database/seeds/RolesTableSeeder.php) file for Insert data in roles table.

Step10(A)- use DB file in RolesTableSeeder.php

use Illuminate\Support\Facades\DB;

Step10(B)- Add Insert Function in run() function of RolesTableSeeder.php add below code.

Step 11: To Add Insert function in UsersTableSeeder.php(database/seeds/UsersTableSeeder.php) file for Insert data in users table.

Step11(A) – use DB file in UsersTableSeeder.php

use Illuminate\Support\Facades\DB;

Step11(B)- Add Insert Function in run function of UsersTableSeeder.php add codes.

Step 12: To Create Database in Mysql Serve to run xampp and go to web browser and follow command

http://localhost/phpmyadmin

And craete database

Step 13: Set Mysql Server Username ,Password and Database Name in .env file and follow command

Step 14: Migration of Tables in Database follow this command in tarminal.

php artisan migration

Step 15:  Define UsersTableSeeder and RolesTableSeeder Class on DatabaseSeeder.php file(database/seeds/DatabaseSeeder.php)

Step 16: Insert data in tables follow this command in terminal.

php artisan db:seed

Step 17: To Create DashboardController for Admin Dashboard work follow this command in terminal.

php artisan make:controller Admin/DashboardController

Step 18: To Create DashboardController for User Dashboard work follow this command in terminal.

php artisan make:controller User/DashboardController

step 19: Make AdminMiddleware for Admin Authentication work follow this command in terminal.

php artisan make:middleware AdminMiddleware

Step 20: Make UserMiddleware for User Authentication work follow this command in terminal.

php artisan make:middleware UserMiddleware

Step 21: Implement condition for Admin login in AdminMiddleware(app/Http/AdminMiddleware.php)

Step21(A) – Use Auth class for Authentication in use head of this  AdminMiddleware.php

use Auth;

Step 21 (B): Implement Condition in handle() function of AdminMiddleware.php added some code

Step 22: Implement condition for User login in UserMiddleware(app/Http/UserMiddleware.php)

Step22(A)- Use Auth class for Authentication in use head of this UserMiddleware.php

use Auth;

Step 22(B):  Implement Condition in handle() function of UserMiddleware.php aded some code

Step 23: Implement Condition for Admin and User Login in RedirectIfAuthenticated(app/Http/Middleware/RedirectIfAuthenticated.php) added some code below

public function handle()

Step 24: Implement Condition Login in LoginController(app/Http/Controllers/Auth/LoginController.php)

Step24(A)- remove = ‘/home’ from protected $redirectTo = ‘/home’;

protected $redirectTo;

And added some code

Step25: Implement Condition Login in ResetPasswordController(app/Http/Controllers/Auth/ResetPasswordController.php)

Step25(A): remove = ‘/home’ from protected $redirectTo = ‘/home’;

protected $redirectTo;

Step 25:  Implement Condition in __construct() function

And added some code

Step 26: Define AdminMiddleware in routeMiddleware(app/Http/Kernel.php)

Step26(A): use AdminMiddleware in Kernel.php File use in the head

use App\Http\Middleware\AdminMiddleware;

Step 26(B): Define AdminMiddleware in routeMiddleware

'admin' => AdminMiddleware::class,

Step 27: Define UserMiddleware in routeMiddleware(app/Http/Kernel.php)

Step27(A): use UserMiddleware in Kernel.php File add in head.

use App\Http\Middleware\UserMiddleware;

Step27(B): Define UserMiddleware in routeMiddleware add on body.

'user' => UserMiddleware::class,

Step 28: Set Middleware and Route in web.php (route/web.php)

Step28(A): Set Admin Middleware and Route added some code.

Step28(B):Set User Middleware and Route added some code.

Step 29: Serve and create link and open it

in terminal

php artisan serve

0 0 votes
Article Rating
Subscribe
Notify of
guest

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