dongqiuqiu4736 2019-01-01 20:17
浏览 135

在Laravel中导航回搜索页面时记住结果

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.

  1. Customers

  2. 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!

  • 写回答

1条回答 默认 最新

  • dououde4065 2019-01-01 20:37
    关注

    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">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起