Home > Back-end >  How to return to another page after finishing process in Laravel?
How to return to another page after finishing process in Laravel?

Time:12-03

This is a little bit hard to understand even the title I put. Sorry about that I just do not know how to clearly explain this, but I will try... So for example I have a controller and inside that controller I have a function which return the data in the table of my database. then in the last column of every row, I make view,add,edit,delete options as links. When a user clicks on the add for example, they will redirect to an add page. After they submit the form of the add page. they should be redirected to the first page that return the data from the table. but the problem is, the variables of foreach loop in the first page do not got recognized by laravel anymore. because they do not got processed since the route does not goes to the controller and to the function that return the data instead it goes to add function.

So I want to know is there anyway to solve this? If you could provide sample code, I would appreciate a lot thanks

CodePudding user response:

From your explanation, I believe the code to go back to the original page after adding, editing etc is simply return redirect()->back(). This will take the user back to the previous page. As for data continuity, one approach would be considering using sessions. They save data globally and can be accessed from any controller once saved. To do this, simply save the data with session(['session_name' => $data]) and to retrieve the data use session('session_name'). Let me know if this helps!

CodePudding user response:

If you want ti redirect after something like a login or an activation process you can do it with something like this:

return redirect()->to('login')

You can specify the path from your web.php file as you can see in my example in 'myPath'

As @DerickMasai correctly pointed out it is better to use named routes instead of hard coding the path itself.

Naming a route can work like so:

Route::get('/login', [LoginController::class, 'index'])->name('login');
  • Related