简体   繁体   English

Laravel-可能在末尾包含分页/查询网址的路由-分页网址未添加到查询网址中,而是对其进行了替换

[英]Laravel - Route that may contain a pagination/query url at the end - Pagination url doesn't add to the query url, it replaces it

So I have a products page where you may browse all products or browse queried products if you choose to. 因此,我有一个产品页面,您可以在其中浏览所有产品,也可以浏览查询的产品。 However in both cases they must have pagination. 但是,在两种情况下,它们都必须具有分页功能。 Here is my route: 这是我的路线:

Route::get('/products/{gender}/{subcategory_name}', [
    'uses' => 'ProductsController@showProducts',
    'as' => 'products'
]);

For the pagination links I use $model->links() in my views. 对于分页链接,我在视图中使用$model->links() Both the pagination and query work. 分页和查询均有效。 When you decide to query the items you must still paginate them. 当您决定查询项目时,仍必须对它们进行分页。 However when I click to go to the second page the pagination url deletes my query url. 但是,当我单击以转到第二页时,分页网址会删除我的查询网址。 The result is loosing the queried products and getting to the second page but with ALL products, not the Queried ones. 其结果是失去所查询的产品和获得的第二页,但所有产品,而不是被查询的。 Here is an step by step walkthrough for clarification: 以下是逐步进行说明的步骤:

  1. I am on this url: products/Men/T-shirt 我在此网址上:产品/男装/ T恤
  2. I click on the second pagination link so I can get to this url: "products/Men/T-shirt?page=2" // This works 我单击第二个分页链接,以便可以访问以下网址: “ products / Men / T-shirt?page = 2” //可行
  3. I decide to query the items. 我决定查询项目。 Here is the url afterwards: "products/Men/T-shirt?color%5B%5D=1" // This works. 之后是以下网址: “ products / Men / T-shirt?color%5B%5D = 1” //可行。 I query the products and get the correct pagination links. 我查询产品并获得正确的分页链接。
  4. Now here is the problem. 现在这是问题所在。 When I click the second pagination link of the queried products the pagination url delets my query url and I go to this link " products/Men/T-shirt?page=2 ". 当我单击查询产品的第二个分页链接时,分页URL删除了我的查询URL,然后转到此链接“ products / Men / T-shirt?page = 2 ”。 Now I am on the second page of ALL not the queried products. 现在我的所有不查询产品的第二页上。 I need to go to the second page of the QUERIED products. 我需要转到查询产品的第二页。

Solutions and advice are welcome. 欢迎提供解决方案和建议。 Thanks in advance! 提前致谢!

You need to tell Laravel to append your own query parameters to the links() method, for example: 您需要告诉Laravel将您自己的查询参数附加到links()方法,例如:

{{ $model->appends(['color' => Request::input('color')])->links() }}

To have the query params dynamically you could use something like: 要使查询参数动态化,您可以使用类似以下内容的方法:

{{ $model->appends(Request::except('page'))->links() }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM