In the previous lecture, we have seen the login register part and in this tutorial, we are going to show how to create middleware and register it. how to apply middleware on group route …..

Create a middleware using the below command

php artisan make:middleware Adminmiddleware

Then go to the  app/http/middleware/Adminmiddleware.php and paste the below code.

 public function handle(Request $request, Closure $next)
      // return $next($request);
       if(Auth::user()->role=='1') //1 means admin
           return $next($request);
           return redirect('/home')->with('status','Access denied you are not admin');
       return redirect('/home')->with('status','Please Login First');

Note: Don't forget to use Auth on top

use Illuminate\Support\Facades\Auth;

Now we have to register the middleware. Go to the app/http/kernel.php file. and register it like below

Under the route middleware

'isAdmin' => \App\Http\Middleware\Adminmiddleware::class,

Now go to the app/http/controllers/Auth/LoginController.php file and comment on the  below part

 // protected $redirectTo = RouteServiceProvider::HOME;

We will create our own redirect rules as per the role.

protected function authenticated(){


        return redirect('dashboard')->with('status','Welcome to your Dashboard');


        return redirect('dashboard')->with('status','Login successfully')


   }//End Method

Now we have to create group middleware and apply our newly created Aminmiddleware

Go to the route folder and open the web.php file and create a group middleware



        return "This is Admin";



Now create one more user and try to login . If the role is admin it will show the message that This is Admin and if the role is user it will redirect to page. Make sure first we have to change our user role for admin as set 1. So go to the phpmyadmin and users table set role as 1.

Thanks for reading………

Advertising Area