Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

avishkaMe's avatar

how to redirect to a custom URL after login laravel 8.x

I have two user roles as admin and agent. I want to redirect agent to another dashboard view and admin to main dashboard after login.

This is my LoginController

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Role;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function authenticated()
{
    if(auth()->user()->hasRole('Admin'))
    {
        return redirect('/home');
    } 

    return redirect('welcome');
}
    
}

This is my User.php

public function hasRole($role)
    {
      if ($this->roles()->where('name', $role)->first()) {
        return true;
      }
      return false;
    }

but after login, I have got this error

Error
Class 'App\Role' not found

Can you please help me to fix this issue?

0 likes
4 replies
martinbean's avatar

@avishkame Try importing the correct class. The error message is telling you there’s no class named App\Role, but in your first code sample you import App\Models\User.

1 like
avishkaMe's avatar

Corrected it, but the error still appearing.

Please or to participate in this conversation.