Laravel middleware returning (Trying to get property 'headers' of non-object) error

In middleware, it is important to handle all cases and return the redirects accordingly or abort.

You do return $next($request); when you want to allow system to continue processing the request ahead.

However, if in case if (Auth::check() && Auth::user()->role == 'title_officer') condition fails, you have not mentioned what system should do.

You can may be abort(404) if you do not want to show the page as available or maybe abort(403) for access forbidden.

public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->role == 'title_officer') {
            return $next($request);
        }
        abort(403);
    }

But make sure you do not add a case which will make an infinite loop. Please check documentation for more options.


just use some thing like this in middleware : RedirectIfAuthenticated

public function handle($request, Closure $next, $guard = null)
{
     if (Auth::guard($guard)->check()) {
         // your condition
         return redirect('the route name');
     }
}