Target class [admin] does not exist.

Updated at 02-Mar-2022, By samar

Target class [admin] does not exist.

Hello everyone, in this post we will examine how to solve the "Target class [admin] does not exist." programming puzzle.

I got error Target class [admin] does not exist while using admin middleware to authenticate admin users because I did not use the admin middleware 'admin' => 'App\Http\Middleware\Admin', in app\Http\Kernel.php file. Hope it will help you to find the solution for you
  • Create Admin middleware to authenticate admin users

    1. Create middleware to authenticate admin users using php artisan command
    php artisan make:middleware Admin
    
    1. Edit middleware Admin.php

    app\Http\Middleware\Admin.php

    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->user_type == 'admin') 
        {
            return $next($request);
        }
        
        else {
            return redirect('/');
        }
    }
    
    1. Add it to the routeMiddleware array in your kernel file

    app/http/Kernel.php

    protected $routeMiddleware = [
        'admin' => 'App\Http\Middleware\Admin',
    ];
    
    1. Use Admin middleware to Route group

    routes\web.php

    Route::middleware(['admin'])->group(function () {
        Route::get('admin/dashboard', '[email protected]')->name('admin.dashboard');
    });
    

Back to code snippet queries related laravel