You are redirecting in controller and redirecting in middleware, why?
You just need:
return $next($request);
If the condition checks out in middleware.
Also https://laravel.com/docs/6.x/middleware#middleware-parameters
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have been trying to fix this problem but to no luck. I have an Agent Model where I want have normal agents and super agents. The normal agents can register benefactors while the super agents can register the agents and also view the normal agent's benefactors. I want to separate the two such that the normal agent cannot access the dashboard of the super agent. So i have created A super agent middleware as follows:
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return redirect()->route('agents.signin');
}
if(Auth::user()->systemAdmin == 0){
return redirect()->route('benefactors.dashboard');
}
if (Auth::user()->systemAdmin == 1) {
return redirect()->route('agents.dashboard');
}
}
}
my AgentLoginController as follows:
class AgentLoginController 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 = '/benefactorDashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'phoneNumber' => 'required',
'password' => 'required',
]);
if(auth()->attempt(array('phoneNumber' => $input['phoneNumber'], 'password' => $input['password'])))
{
if (auth()->user()->is_admin == 1) {
return redirect()->route('agents.dashboard');
}else{
return redirect()->route('benefactors.dashboard');
}
}else{
return redirect()->route('agents.signin')
->with('error','Phone Number or Password Are Wrong.');
}
}
}
I keep getting *Too may redirects * :(
is there a way I can fix this ? :)
here is my agents Table
Schema::create('agents', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('surname');
$table->string('firstName');
$table->string('secondName');
$table->string('idNumber')->unique();
$table->string('phoneNumber');
$table->string('email')->nullable();
$table->string('password');
$table->boolean('systemAdmin')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
Please or to participate in this conversation.