带有搜索参数的 Laravel 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18137011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel pagination with search parameters
提问by vikingsfan19
I've having trouble preserving my search parameters when clicking on a pagination link. For example, if a search query returns 40 records and I have two pages, clicking on the second page will return the second page of the full set of records instead of just the 40 that were returned by the search.
单击分页链接时,我无法保留我的搜索参数。例如,如果搜索查询返回 40 条记录并且我有两页,单击第二页将返回完整记录集的第二页,而不仅仅是搜索返回的 40 条记录。
Here is the postIndex()
from my controller:
这是postIndex()
来自我的控制器:
public function postIndex(){
$validator = Validator::make(
Input::all(),
array('priceMin' => array('numeric'),
'priceMax' => array('numeric')
)
);
if ($validator->fails()){
return Redirect::to('items')->withInput()->withErrors($validator);
} else {
return Redirect::to('items')->withInput();
}
}
And my getIndex()
:
而我的getIndex()
:
public function getIndex(){
$items= $this->retriever->getListings(Input::old(), 20);
return View::make('listings', array('items' => $items);
}
The retriever
object then loops through the old input and finds all valid search parameters, queries the database with them, and paginates with the specified amount, 20 in this case.
retriever
然后该对象遍历旧输入并找到所有有效的搜索参数,用它们查询数据库,并使用指定的数量(在本例中为 20)进行分页。
I've tried to use ->appends()
but then the data isn't in Input::old()
and it makes for a terrible url if there are 10 search parameters since it uses GET
and not POST
. How can I get my parameters applied to the pagination links?
我试过使用,->appends()
但数据不在其中,Input::old()
如果有 10 个搜索参数,因为它使用GET
而不是POST
. 如何将我的参数应用于分页链接?
回答by Andreyco
I would never ever use POST request to do any filtering/sorting/searching!!! This is just wrong
我永远不会使用 POST 请求来做任何过滤/排序/搜索!!!这是错误的
In general, if user tries to reload page (search result page) that some data (search params) has been sent to, browser asks whether it should reload and resend that data again. I must not say, this is annoying, at least.
通常,如果用户尝试重新加载某些数据(搜索参数)已发送到的页面(搜索结果页面),浏览器会询问是否应重新加载并重新发送该数据。我不能说,这很烦人,至少。
Solution: stick with Laravel method and use appends()
despite the fact it generates 'ugly' URL - this is a common solution, regardless the framework.
解决方案:坚持使用 Laravel 方法并使用,appends()
尽管它会生成“丑陋”的 URL - 这是一个常见的解决方案,无论框架如何。