Using Role Based Authentication, Create Different Users DashBoard


i. Create a project in C:/xamp/htdocs directory by using Git Bash. To create a new project run the following code in Git Bash terminal

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

ii. Open the project in your code editor like VS Code

Step-1 : To use User Authentication in your project run the following code in code editor terminal

php artisan make:auth

Step-2 : Customize users table to add role field in the table. Go to database/migration/2021_03_20_000000_create_users_table.php and make some changes in users table.

Step-3: Create a Role Model and Migration for Role Table by using the following code:

php artisan make:model Role -m

Step-4: Now customize the Role table and add role_id and role_name field .Go to database/migration/2020_03_20_060108_create_roles_table and make some changes in up() function.

Step-5: Make Relationship between Users and Roles tables through Role Model and User Model.

i. To create relationship make changes in users( ) function in Role.php.

ii. In User.php make a role( ) function and write the following code .

Step-6 :  Create UsersTableSeeder  and RolesTableSeeder file by writting following two command in terminal.

php artisan make:seed UsersTableSeeder
php artisan make:seed RolesTableSeeder 

Step-7: Add Insert function in UsersTableSeeder.php and RolesTableSeeder.php file for Insert data in users table and role table. Add ” use Illuminate\Support\Facades\DB; ” in use area.

i. Go to database/seeds/UsersTableSeeder.php and add the following insert operation in run( ) function :

ii. Go to database/seeds/RolesTableSeeder.php and add the following insert operation in run( ) function :

Step-8 : Create a new Database and link it with the project

i. Run Apache and MySql .

ii. Open phpmyadmin/localhost in your web browser.

iii. Create a new database and set its name.

iv. Set Mysql Server Username ,Password and Database Name in .env file

DB_DATABASE=multipleuser (Your DB name)

v. Migrate it by following command:

php artisan migration

Step-9 : Insert data in the table

php artisan db:seed

Step-10: Create DashboardController for Admin Dashboard work and User Dashboard work.

i. For Admin Dashboard

php artisan make:controller Admin/DashboardController

ii. For User Dashboard

php artisan make:controller User/DashboardController

Step-11 : Make AdminMiddleware and UserMiddleware for Admin and User Authentication work.

i. For AdminMiddleware

php artisan make:middleware AdminMiddleware

ii. For UserMiddleware

php artisan make:middleware UserMiddleware

Step-12: Go to app/Http directory and Use Auth class for Authentication in AdminMiddleware.php and UserMiddleware.php in use section. Implement the following condition in handle( ) function:

i. In AdminMiddleware.php add the following codition:

i. In UserMiddleware.php add the following codition:

Step-12: Go to app/Http/Middleware/RedirectIfAuthenticated.php . Implement Condition for Admin and User Login in RedirectIfAuthenticated.php. and not forget to put ” use Auth; ” in use section.

Step-13: Go to app/Http/Controllers/Auth/ directory and remove = ‘/home’ from protected $redirectTo = ‘/home’; for ResetPasswordController.php and LoginController.php . Implement Condition Login in both php file in __construct( ) function.

i. In LoginController.php

ii. In ResetPasswordController.php

Step-14.A : Use AdminMiddleware and UserMiddleware in Kernel.php File in app/Http/Kernel.php directory

i. For AdminMiddleware

use App\Http\Middleware\AdminMiddleware;

ii. For UserMiddleware

use App\Http\Middleware\UserMiddleware;

Step-14.B : Define AdminMiddleware and UserMiddleware in routeMiddleware

i. For AdminMiddleware

'admin' => AdminMiddleware::class,

ii. For UserMiddleware

use App\Http\Middleware\UserMiddleware;

Tagged : /

CRUD Operation Using Laravel

CRUD is an short form for Create, Read, Update, and Delete. Using this operation users can easily manipulate the data.

To preform CRUD operation we have to preform certain steps :

Step 1 : Make a new Project .

To create a new project, run the following code in Git Bash in C:/xamp/htdocs Folder:

composer create-project –prefer-dist laravel/laravel project_name “version_name.*”

Eg: composer create-project –prefer-dist laravel/laravel Employees “5.8.*”

It will automatically install all the necessary file and folder for you.

Step 2 : Create a Database in SQL

Run Xamp Controller and start Apache and MySQL.

Search localhost/phpmyadmin in your browser

Create a new database using MySQL by simply click in New button and give your database a unique name.

Step 3 : Modify the Database connection in .env File

Open the .env file and change database name and set username to “root”.




DB_DATABASE= your database name

DB_USERNAME=you username(Default: root)


Step 4: Link the project with database

To link database run the following command on Command Promt/Git Bash/Vs Code Terminal:

php artisan migrate

Step 5: Create Table in database

We are going to create crud application for product. so we have to create migration for “products” table using Laravel 5.8 php artisan command

php artisan make:migration create_products_table –create=products

Step 6: Migration

Step 7: Create Blade File

Go to Project_name/resources/views and create the following file:

i . parent.blade.php

ii. create.blade.php

iii. view.blade.php

iv. edit.blade.php

v. index.blade.php

Step 9 : Set Route

Go to Project_name/route/web.php add the following code to change the route to universal route

We can easily change the route by changing the route name.