0

This is mine LoginController

 protected $redirectTo;
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    if(Auth::check() && Auth::user()->role->id == 1){
        $this->redirectTo=route('admin.dashboard');
    }
    else{
        $this->redirectTo=route('user.dashboard');
      }

    $this->middleware('guest')->except('logout');
}

} Here is mine web.php

Route::get('/', function () {
    return view('welcome');
})->name('home');

Auth::routes();

I don't know Why after Successfully logging in its going to / route i want to send it to another route which after logging in i manually Enter the route i successfully viewed that route if i am logged in its not going to show but how can i manage after logging in route

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Asad
  • 617
  • 2
  • 8
  • 23

1 Answers1

0

You are doing it wrong. You shouldn't put such code into controller constructor because it won't probably work.

Instead in your controller you should define custom redirectTo method:

protected function redirectTo()
{
   if(Auth::check() && Auth::user()->role->id == 1) {
       return route('admin.dashboard');
   }

   return $this->redirectTo=route('user.dashboard'); 
}

This should work because in Illuminate/Foundation/Auth/RedirectsUsers.php trait there is method:

public function redirectPath()
{
    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

defined that is used later in LoginController by default when user was succesfully logged.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291