Laravel-将用户重定向到个人资料页面

I am trying to redirect the user if he logs in for the first time to step2 page, I have tried the following

    public function postLogin(Request $request){

    $credentials = $request->only('login_email', 'login_password');
    $credential = ['email'=> $credentials['login_email'], 'password' => $credentials['login_password']];
    if (Auth::attempt($credential)) {
        // if profile not set, redirect to step 2 page
        if(Auth::user()->first_login) {

          return  $this->getStep2(Auth::user()->id);


        }
}

but it shows me

{"login":true}

My getStep2() is like

    public function getStep2($id){
    $genres = Track::orderBy('genre', 'asc')->groupBy('genre')->get();
    $countries = Country::all();
    $categories = Category::where('parent_id',  '')->get();
    $user_id = $id;
    return view('users.step2', compact('genres', 'countries', 'categories', 'user_id'));
}

If you want to redirect, you should

return redirect('users/step2');

and then, in your routes.php have this route

Route::get('users/step2', 'UserController@getStep2');

Observe that you don't need to actually pass the user id as a parameter, since you can access it using the Auth facade.


If what you are trying to do is actually load a view, then your approach should do just that. My guess is that one of your methods is ended before reaching the return statement.

You can also use the redirect()->action(...) method.

public function postLogin(Request $request) {

    $credentials = $request->only('login_email', 'login_password');
    $credential = ['email' => $credentials['login_email'], 'password' => $credentials['login_password']];

    if (Auth::attempt($credential)) {
        // if profile not set, redirect to step 2 page
        if (Auth::user()->first_login) {
            return redirect()->action('Auth\AuthController@getStep2');

        }
    }

    return redirect('/');
}

Note that you still have to create a route for this page.

Route::get('step-two', ['uses' => 'Auth\AuthController@getStep2']);

For accessing the current user you can use the Auth::user() method.