OK I have a logincontroller with if statements when logging in below. I also have roles being pulled from my user model. And I have the routes to go with them but I am getting route not found when logging in.
Not sure why
Routes
Route::get('/dashboard',[RegistrarController::class,'getRegistrarDashboard'])->middleware('auth')->name('registrar.dashboard');
Route::get('/dashboard',[WebmasterController::class,'getWebmasterDashboard'])->middleware('auth')->name('webmaster.dashboard');
Route::get('/dashboard',[TeamController::class,'getTeamDashboard'])->middleware('auth')->name('team.dashboard');
Route::get('/dashboard',[SponsorController::class,'getSponsorDashboard'])->middleware('auth')->name('sponsor.dashboard');
Route::get('/dashboard',[PastorController::class,'getPastorDashboard'])->middleware('auth')->name('pastor.dashboard');
Route::get('/dashboard',[PilgrimController::class,'getPilgrimDashboard'])->middleware('auth')->name('pilgrim.dashboard');
Route::get('/dashboard',[UserController::class,'getUserDashboard'])->middleware('auth')->name('user.dashboard');
Login Controller
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use auth;
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
*/
// protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function redirectTo()
{
if(Auth::user()->hasRole('registrar'))
{
$this->redirectTo = route('registrar.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('webmaster'))
{
$this->redirectTo = route('webmaster.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('sponsor'))
{
$this->redirectTo = route('sponsor.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('team'))
{
$this->redirectTo = route('team.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('pastor'))
{
$this->redirectTo = route('pastor.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('pilgrim'))
{
$this->redirectTo = route('pilgrim.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('user'))
{
$this->redirectTo = route('user.dashboard');
return $this->redirectTo;
}
}
}
User Model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
public function hasAnyRoles($roles){
if($this->roles()->whereIn('name',$roles)->first()){
return true;
}
return false;
}
public function hasRole($role){
if($this->roles()->where('name',$role)->first()){
return true;
}
return false;
}
}