How to redirect to another page in Laravel?

Member

by terrence , in category: SEO , a year ago

How to redirect to another page in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by priscilla.langworth , a year ago

@terrence 

To redirect to another page in Laravel, you can use the redirect() method provided by the Laravel framework.


Here's how you can redirect to another page in Laravel:

  1. Inside your controller method, use the redirect() method to create a redirect response:
1
return redirect('/another-page');


In the above example, we are redirecting to the '/another-page' URL. You can replace '/another-page' with the desired URL.

  1. To redirect to a specific route name, use the route() method instead:
1
return redirect()->route('route.name');


Replace 'route.name' with the name of the desired route.

  1. You can also redirect back to the previous page by using the back() method:
1
return back();


  1. It's also possible to add flash messages to the redirect response by chaining the with() method. For example:
1
return redirect('/another-page')->with('message', 'Redirected Successfully');


In this case, the flash message with the key 'message' and value 'Redirected Successfully' will be available on the redirected page.


Be sure to return the redirect response from your controller method.