0
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 2

3

After user login you are redirecting the users to a route and that route points to a function, so basically you can implement this with a middleware or a simple function of redirect instead of constructor which is to initialize an object's properties.

 public function redirect()
    {
        $role = Auth::user()->role;
        return match ($role) {
            'Admin' => redirect('/admin/dashboard'),
            'Operator' => redirect('/operator'),
        };
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

def would need a middleware if you want to access the session in the constructor ... the only thing is they are trying to return a string from a middleware, which is a big no no ... nice to see you active my friend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.