I am trying to create a login system where both admin and users can log in using the same form. The problem is when I log in with admin credentials, the URL redirects correctly to admin/dashboard
, but I see the error:
Target class [admin] does not exist
Here is the relevant code:
Admin Controller
middleware('admin');
}
public function dashboard()
{
return view('admin.dashboard');
}
}
User Dashboard Controller
middleware('auth');
}
public function index()
{
$user = auth()->user();
$recentOrders = $user->cartItems()
->with('product')
->latest()
->take(5)
->get();
return view('dashboard', compact('recentOrders'));
}
}
Authenticated Session Controller
authenticate();
$request->session()->regenerate();
if (Auth::user()->isAdmin()) {
return redirect()->intended(route('admin.dashboard'));
}
return redirect()->intended(route('dashboard'));
}
public function destroy(Request $request): RedirectResponse
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect("https://stackoverflow.com/");
}
}
User Model
'datetime',
'password' => 'hashed',
'is_admin' => 'boolean',
];
public function cartItems()
{
return $this->hasMany(CartItem::class);
}
public function isAdmin()
{
return $this->is_admin;
}
}
Kernel Middleware
[
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
}
Routes
name('home');
require __DIR__.'/auth.php';
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
});
I’ve confirmed that the middleware and route definitions seem fine. What am I missing here? How can I fix the Target class [admin] does not exist
error?
Any help or guidance would be greatly appreciated!
I am tired to find out where the problem is occur.