I have created a search page using Laravel that has "text to search" filters at the top. When the mouse is taken off the filter the page's JavaScript fires an Ajax call then returns the results.
As I also have pagination enabled (with Ajax), when I click on page 2 (a link that looks like: http://project.test/customers?page=2) the filters at the top keep any previously entered text, and the search results are held, which is just what I need!
Now the problem:
The database I have has two main tables.
Customers
Purchases
Relationships: A customer can have many purchases Any given purchase though belongs to a single customer
I've built a search page so that the user can search for customers that match the given search criteria (e.g., those belonging to a particular city, or country, for example).
In the table below the search filters that shows all the matching customer records, I have a column, with a purchases button that then takes the user to that particular customer's purchase page (here, the purchases button goes a link of the form project.test/customers/{{id}}
The {id} goes through a route, that then loads a new page that shows all the purchases of that particular customer.
Now, I have a "go back" button on this purchase page, that goes the following link:
project.test/customers
This then loads the original search page (but loads it afresh).
THIS IS THE PROBLEM!!!
Because the page is loaded afresh (using the link: project.test/customers)
The search filters are cleared, and the page instead shows ALL the customers. And so, the user then has to re-enter all the search criteria again - which is annoying for the user, as he / she will often need to change a batch of purchase records, across a given set of customers, that match a given set of search criteria - and would rather not have to re-enter the same criteria, again, and again, when navigating away to a customer's purchase page, and then navigating back to the main search page.
Given this problem, my question is, is it possible, conceptually, with Laravel, to go to another page, from a search page, and then, using some method to go back to the original search page, for the search filters, and results, showing from before, to still be there? - This would be a super useful thing to have!
I've seen this done on other sites, possibly using other web technologies, but I'm just unsure how to start / where to begin to do this with Laravel / PHP?
I appreciate any help, thanks in advance!
Store the search criteria in the user's session, passing them back to the search page when they navigate back.
Say you have a controller with a function for the search route:
public function search (Request $request)
{
// store the search criteria
session(['search_criteria' => $request->input()]);
// return the results
}
Then within another controller function, add the original parameters to the response:
public function someRouteMethod (Request $request)
{
return redirect()->with('search_criteria', session('search_criteria'))->to('/search');
}
Then in the search page repopulate the form with the $search_criteria
data:
<input value="{{ !empty($search_criteria) ? $search_criteria['foo'] : '' }}" name="foo">