Laravel 4 中的分页适用于一页。但不为另一个人工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17451963/
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
Pagination in Laravel 4 works for one page. but not working for another
提问by harishannam
I downloaded the starter kit for laravel using sentry from GitHub.
我使用 GitHub 上的 sentry 下载了 laravel 的入门套件。
Everything is fine so far. Blog page has pagination. I am able to change the number of items per page as I like by changing the same pagination(x) value.
到目前为止一切都很好。博客页面有分页。我可以通过更改相同的分页(x)值来随意更改每页的项目数。
Now problem is, i created a new page manually by myself called "Search" which displays results for Keywords searched.
现在的问题是,我自己手动创建了一个名为“搜索”的新页面,该页面显示搜索关键字的结果。
Here I added the same pagination, it shows the number of items per page as mentioned, page numbers, arrows. Everything is perfect, but when I click page 2 it shows a BLANK PAGE. The page source of blank page is also empty. The URL is perfect for page two. It generates correctly as
在这里我添加了相同的分页,它显示了每页提到的项目数、页码、箭头。一切都很完美,但是当我单击第 2 页时,它显示了一个空白页。空白页的页源也是空的。该 URL 非常适合第二页。它正确生成为
sitename.dev/search?page=2
sitename.dev/search?page=2
Please guide me where I am going wrong. The blog page pagination still works fine. Problem occurs only in newly created SEARCH page pagination.
请指导我哪里出错了。博客页面分页仍然可以正常工作。问题仅出现在新创建的 SEARCH 页面分页中。
This is my 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');
}
}
This is my Route
这是我的路线
Route::any('/search', 'BlogController@postSearch');
采纳答案by Israel Ortu?o
First of all
首先
return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
When doing compact
you're already including the $posts
var to be used as $posts
in the view, exactly the same you'd do using with('posts', $posts)
. So this last can be removed. If you want to load more vars using compact
just separe the variable names by commas.
执行此操作时,compact
您已经包含$posts
要$posts
在视图中使用的var ,与使用with('posts', $posts)
. 所以这最后一个可以删除。如果您想加载更多变量,compact
只需使用逗号分隔变量名称。
You're paginating the item in the POST
action of Search
, when you click any of the pagination links or call page=2
you're not running the postSearch
method, you're probably calling getSearch
as you're not doing now a POST
request.
您在 的POST
操作中对项目进行分页Search
,当您单击任何分页链接或调用page=2
您没有运行该postSearch
方法时,您可能正在调用,getSearch
因为您现在没有执行POST
请求。
Have a look to RESTful controllers
回答by Yoga Hanggara
Search form better using GET method, because it's make easy to bookmark/save the URL for later use. In Laravel 4's Pagination, there's a method to appends the query string. So, we can keep the search querystring in other pages.
使用 GET 方法更好地搜索表单,因为它可以轻松地将 URL 加入书签/保存以供以后使用。在 Laravel 4 的 Pagination 中,有一种附加查询字符串的方法。因此,我们可以将搜索查询字符串保留在其他页面中。
Docs: http://laravel.com/docs/pagination#appending-to-pagination-links
文档:http: //laravel.com/docs/pagination#appending-to-pagination-links
My code:
我的代码:
Search Form
搜索表格
<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>
Routing
路由
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 / BladeAt method to display pagination:
View / BladeAt 方法显示分页:
{{ $posts->appends(array('q' => $search))->links() }}
So we can keep the q=?querystring in other pages (ex: /lib/search?page=2&q=apple)
所以我们可以保留q=? 其他页面中的查询字符串(例如:/lib/search?page=2&q=apple)
回答by Tim F
A few things to check.
有几件事要检查。
- The new route exists in the
app/routes.php
file. - You are returning a view file for the new route.
- That view file has the pagination output. For example:
<?php echo $articles->links(); ?>
.
- 新路由存在于
app/routes.php
文件中。 - 您正在返回新路线的视图文件。
- 该视图文件具有分页输出。例如:
<?php echo $articles->links(); ?>
。
Ultimately, there appears to be some kind of error occurring, which is why the page is rendering blank. Trying turning php errors on in your dev env and see what happens after that.
最终,似乎发生了某种错误,这就是页面呈现空白的原因。尝试在您的开发环境中打开 php 错误,然后看看会发生什么。
回答by harishannam
appends solved my issue. Also usage of appends will enable our users to save/bookmark the URL for future use.
附加解决了我的问题。此外,附加的使用将使我们的用户能够保存/标记 URL 以备将来使用。
But one problem is, it is not appending First page of the result.
但一个问题是,它没有附加结果的第一页。