Laravel 5.5 conditional redirect on register

Viewed 1102

I am using Laravel 5.5 and I want to redirect the uses when they register on different route depending on whether it has custom session or not I managed to do that in the LoginController like so:

protected $redirectTo = '/';

    protected function redirectTo()
    {
        if (Session::has('userRequest')) {
            return route('request');
        }

        if (Auth::user()->role->name == 'admin') {
            return route('admin-dashboard');
        }

        if (Auth::user()->role->name == 'dealer') {
            return route('my-requests');
        }
    }

But when I try this in the RegisterController:

protected $redirectTo = '/';

    protected function redirectTo()
    {
        if (Session::has('userRequest')) {
            return route('request');
        }
    }

It throws Object of class Illuminate\Routing\Redirector could not be converted to string exception

1 Answers

Method redirectTo must return a route/string. When the session does not have userRequest, the method returns null causing this error.

edit the method to be:

protected function redirectTo()
{
    if (\Session::has('userRequest')) {
        return route('request');
    }
    return $this->redirectTo; // or any route you want.
}
Related