带有搜索参数的Laravel分页


Laravel pagination with search parameters

单击分页链接时,我在保留搜索参数时遇到了问题。例如,如果一个搜索查询返回40条记录,而我有两个页面,单击第二个页面将返回完整记录集的第二页,而不是搜索返回的40条。

这是来自我的控制器的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();
    }
}

我的getIndex():

public function getIndex(){
    $items= $this->retriever->getListings(Input::old(), 20);
    return View::make('listings', array('items' => $items);
}

然后,retriever对象循环遍历旧输入,找到所有有效的搜索参数,用它们查询数据库,并用指定的数量分页,在本例中为20。

我试过使用->appends(),但数据不在Input::old()中,如果有10个搜索参数,这将导致糟糕的url,因为它使用GET而不是POST。如何将我的参数应用于分页链接?

我永远不会使用POST请求来进行任何筛选/排序/搜索!!!这只是错误的

通常,若用户试图重新加载某些数据(搜索参数)已发送到的页面(搜索结果页面),浏览器会询问是否应该重新加载并重新发送该数据。我不能说,至少这很烦人。

解决方案:坚持使用Laravel方法并使用appends(),尽管它生成了"丑陋"的URL——这是一个常见的解决方案,无论框架如何。