skip to Main Content
public function __construct()
{
     if (Auth::check()){
        if(Auth::user()->user_type == 'admin'){
            return to_route('admin.dashboard');
        } elseif(Auth::user()->user_type == 'operator'){
            return to_route('operator.dashboard');
        }
     }
}

I am checking user authentication.

2

Answers


  1. Create a controller method, don’t do it in the constructor. Constructors in Laravel controllers do not typically return values.

    public function someMethod()
    {
        if (Auth::check()) {
            if (Auth::user()->user_type === 'admin') {
                return redirect()->route('admin.dashboard');
            } elseif (Auth::user()->user_type === 'operator') {
                return redirect()->route('operator.dashboard');
            }
        }
    
        //...
    }
    
    Login or Signup to reply.
  2. It’s not possible by design. If you need it you can create middleware in constructor like this:

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (Auth::check()){
               if(Auth::user()->user_type == 'admin'){
                  return to_route('admin.dashboard');
               } elseif(Auth::user()->user_type == 'operator'){
                  return to_route('operator.dashboard');
               }
             }
     
             return $next($request);
        });
    }
    

    but you can also create normal middleware and use it for controller.

    See this Laravel change to get more explanation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search