Home » Php » php – Laravel call route from controller

php – Laravel call route from controller

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am calling getting_started route after successfully login :

protected $redirectTo = '/getting_started';

Here is my getting_started route code :

Route::get('/getting_started','[email protected]_started');

And controller code :

public function getting_started()
{
    $id= Auth::id();
    $user = DB::table('user_profiles')->where('user_id', '=', $id)->first();

    if($user->dashboard_access == 0)
    {
        DB::table('user_profiles')
            ->where('user_id', $id)
            ->update(['dashboard_access' => 1]);
        return view('user.getting_started');
    }

    return view('user.dashboard');
}

It works perfectly and show in url :

http://localhost:8080/getting_started

Now I actually want that if user.dashboard view is call it show in url like :

http://localhost:8080/dashboard`

And on getting_started view show :

http://localhost:8080/getting_started

It is possible to call dashboard route instead of :

  return view('user.dashboard');

My dashobard route is :

Route::get('/dashboard',['middleware' => 'auth', function () {
    return view('user.dashboard');
}]);
How to&Answers:

What I understand it is that you are looking for is this function

return redirect()->route('dashboard');

It’s my understanding of your question which can be wrong. Maybe you are asking something else.

Answer:

That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :

return redirect()->route('user.dashboard');

Hope this helps.