In this tutorial, I will let you know the easy solution to create middleware to make sure if logged user has the privileges for admin.
I have user table having column name "user_type" that manage the status of the users.
Please follow the steps to handle admin middleware :Add Middleware
First i will create
AdminMiddleware.php in following path app/Http/Middleware/AdminMiddleware.php
- namespace App\Http\Middleware;
- use Closure;
- class AdminMiddleware
- * Handle an incoming request. User must be logged in to do admin check
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- public function handle($request, Closure $next)
- if (\Auth::user()->user_type == 'Admin')
- return $next($request);
- return redirect()->guest('/');
After creating middleware don't forget to register the middleware as
routeMiddleware in app/Http/Kernel.php
- protected $routeMiddleware = [
- 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
- 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
- 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
- 'can' => \Illuminate\Auth\Middleware\Authorize::class,
- 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
- 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
- 'admin' => \App\Http\Middleware\AdminMiddleware::class,
Now you have successfully configured the admin middleware.
Ok, let's assign this admin middleware in
In this step, we will add some routes within the admin middleware to check it is working fine or not.
- Route::group(array('prefix'=>'administration','middleware' => ['auth', 'admin']), function ()
- return "Welcome to Administration";
If you want to implement multi auth with multi models in Laravel 5.4 then follow the link :How to implement multi auth in Laravel 5.4 with example