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

CRUD OPRATION USING AJAX

  • Read data from a web server – after a web page has loaded
  • Update a web page without reloading the page
  • Send data to a web server – in the background

step 1: install composer
goto terminal and write over here
-> composer install

Step 2: Create .evn file

goto terminal and type

->php .env.example .env

Step 3: To migration

php artisan migrate file_name

Step 4: All source file

i. .evn file

ii. Model file

iii. Migration file

iv. Controller file

v. View file

vi. posts file

vii. Routes file

Step 5: Artisan serve

php artisan serve

LARAVEL CRUD

Step 1: make project.

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

Step 2 :Create datbase to php

Goto web browser and type url localhost/phpmyadmin and make database

Step 3: Update Database Configuration

We will make database configuration for example database name, username, password etc for our crud application of laravel 5.8.
So let’s open .env file and fill all details like as bellow

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=

Step 4: Link with database

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 : View and make blade file

first go resources than view and make blade file.

i . parent.blade.php

ii. create.blade.php

iii. view.blade.php

iv. edit.blade.php

v. index.blade.php

Step 9 : Route

goto routes and open web.php an make routes

i. universal route

Route::resource ('model_name' , 'Controller_name');

ii. indivisual route