[FIXED] Middleware, how to redirect after check Laravel 5


I need after check if user is logged as editor, to redirect to profile page…

Here is my code:

<?php namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

use Closure;

class AdminMiddleware {

     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)
        if (Auth::check()) {
            if (Auth::user()->roles->toArray()[0]['role'] == 'editor'){
                return redirect('/profile');
                return $next($request);
            else { 
                return $next($request);

Problem with this code is when user is editor I get infinite loop….

Here is my routs:

Route::group(['middleware' => 'auth'], function(){

    Route::get('home', ['middleware' => 'admin', function()
        return view('home');
    Route::get('profile', array(
       'as' => 'profile',
       'uses' => 'UserController@getProfile'


Anyone know what is problem?


Where did you register your middleware in App\Http\Kernel?

Is it in protected $middleware = [] or protected $routeMiddleware = [] ?

If registered in $middleware it will run on each very request thereby causing infinite loop, if so use only $routeMiddleware

Answered By – Emeka Mbah

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published