我使用 sentry 从 GitHub 下载了 laravel 入门工具包。
到目前为止一切都很好。博客页面有分页。通过更改相同的 pagination(x) 值,我可以根据需要更改每页的项目数。
现在的问题是,我自己手动创建了一个名为“搜索”的新页面,它显示搜索关键字的结果。
我在这里添加了相同的分页,它显示了每页提到的项目数、页码、箭头。一切都很完美,但是当我点击第 2 页时,它显示的是空白页。空白页的页源也是空的。该 URL 非常适合第二页。它正确生成为
sitename.dev/search?page=2
请指导我哪里出错了。博客页面分页仍然可以正常工作。问题仅出现在新创建的 SEARCH 页面分页中。
这是我的 Controller
public function postSearch()
{
$searchString = Input::get('searchInput');
$posts = Post::where('title', 'LIKE', '%' . $searchString . '%')->orderBy('created_at', 'DESC')->paginate(4);
if($searchString){
return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
}
else{
return Redirect::route('home')->with('error', 'Please enter Search Term');
}
}
这是我的路线
Route::any('/search', 'BlogController@postSearch');
最佳答案
使用 GET 方法可以更好地搜索表单,因为它可以很容易地为 URL 添加书签/保存供以后使用。在 Laravel 4 的分页中,有一种方法可以附加查询字符串。因此,我们可以将搜索查询字符串保留在其他页面中。
文档: http://laravel.com/docs/pagination#appending-to-pagination-links
我的代码:
搜索表单
<form method="get" action="{{{ URL::to('lib/search') }}}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
<div class="controls">
<input type="submit" class="btn" id="submit" value="Submit" />
</div>
</div>
路由
Route::get('lib/search', 'LibraryController@getSearch');
Controller
public function getSearch()
{
$search = Input::get('q');
$posts = $this->post->where('title', 'like', '%'.$search.'%')->paginate(10);
return View::make('site/libraries/list', compact('posts', 'search'));
}
View / Blade 显示分页的方法:
{{ $posts->appends(array('q' => $search))->links() }}
所以我们可以在其他页面中保留 q=? 查询字符串(例如:/lib/search?page=2&q=apple)
关于php - Laravel 4 中的分页适用于一页。但不为另一个人工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17451963/