For this project we consider two types of user - admin and customer. Their roles are assigned as 1 and 2 statically. Now we need to carry out mentioned steps below.
Step 01: Setting up routes/web.php for process login
Route::post('/login', [LoginController::class,'process_login'])->name('login');
Step 02: Changing Up Auth/LoginController.php adding process_login() method
public function process_login( Request $request ) { $request->validate( [ 'email' => 'required', 'password' => 'required', ] ); $credentials = $request->except( ['_token', 'remember_me'] ); $user = User::where( 'email', $request->email )->first(); $remember_me = ( !empty( $request->remember_me ) ) ? TRUE : FALSE; if ( auth()->attempt( $credentials ) ) { //dd(auth()->attempt($credentials)); Auth::login( $user, $remember_me ); if(auth()->user()->role_id == 1 ): return Redirect::intended(route( 'products.index' )); else: return Redirect::intended(route( 'orders.index' )); endif; } else { return back()->with( 'error', 'Invalid credentials' ); } }
Step 03: Now adding changes in web/routes.php again for resource routes for product and order page which will have index page where it will be redirected to after login. Also we will be using auth middleware to prevent it accessing without authentication
Route::group(['middleware'=> [ 'auth']], function() { Route::resource('products', \App\Http\Controllers\ProductController::class); Route::resource('orders', \App\Http\Controllers\OrderController::class); });
Step 04: Now we will be making product and order controller with artisan command
php artisan make:controller ProductController php artisan make:controller OrderController
Step 05: Now adding index() method in both the controller and populating the method with relevant eloquent query code. Please note during our seeding step we also have created models for product and order as well as other resources.
// ProductController.php // at the top use App\Models\Product; // public function index( Request $request ) { $meta_title = 'All Products'; $query = Product::query(); $products = $query->orderBy( 'id', 'desc' )->paginate( 10 )->withQueryString(); return view( 'products.list', compact( 'meta_title','products' ) ); } // OrderController.php // at the top use App\Models\Order; // public function index( Request $request ) { $meta_title = 'All Orders'; $query = Order::query(); $orders = $query->orderBy( 'id', 'desc' )->paginate( 10 )->withQueryString(); return view( 'orders.list', compact( 'meta_title','orders' ) ); }
Step 06: Now adding relevant list() view method under corresponding resources/view folder
// list.blade.php under products folder @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <table class="table mt-3"> <h2>Client Product Details</h2> <thead class="table-dark"> <tr> <th>Name</th> <th class="text-center">Id</th> <th class="text-center">Name</th> <th class="text-center">Cost</th> <th class="text-center">Created</th> <th class="text-center">Order</th> <th class="text-center">Actions</th> </tr> </thead> <tbody> <tr> <td>Denton Bruce</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> </tr> </tbody> </table> </div> </div> </div> @endsection // list.blade.php under orders folder @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <h2>Client Order Details</h2> <table class="table mt-3"> <thead class="table-dark"> <tr> <th class="text-center">Id</th> <th class="text-center">Number of items</th> <th class="text-center">Company</th> <th class="text-center">Phone no</th> <th class="text-center">job no</th> <th class="text-center">Email sent?</th> <th class="text-center">Created</th> <th class="text-center">Actions</th> </tr> </thead> <tbody> <tr> <td>Denton Bruce</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> <td class="text-center">demo</td> </tr> </tbody> </table> </div> </div> </div> @endsection
Now if we attempt to login with right credentials we will see demo listing of product or order page based on role. In our process_login() method we can see for role 1 we are redirecting to product page ( admin being role 1 ) and otherwise.