@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.
Jan 31, 2021
4
Level 1
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?
Please or to participate in this conversation.