Laravel 4中的分页仅占一页.但不为另一个工作 [英] Pagination in Laravel 4 works for one page. but not working for another
问题描述
我使用GitHub上的哨兵下载了laravel入门套件.
I downloaded the starter kit for laravel using sentry from GitHub.
到目前为止,一切都很好.博客页面具有分页.通过更改相同的分页(x)值,我可以根据需要更改每页的项目数.
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.
现在的问题是,我自己创建了一个名为搜索"的新页面,该页面显示了搜索到的关键字的结果.
Now problem is, i created a new page manually by myself called "Search" which displays results for Keywords searched.
在这里,我添加了相同的分页,它显示了如上所述每页的项目数,页码,箭头.一切都很完美,但是当我单击第2页时,它显示为空白页.空白页的页面来源也为空.该网址非常适合第二页.正确生成为
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
sitename.dev/search?page=2
请指导我哪里出问题了.博客页面分页仍然可以正常工作.仅在新创建的SEARCH页面分页中出现问题.
Please guide me where I am going wrong. The blog page pagination still works fine. Problem occurs only in newly created SEARCH page pagination.
这是我的控制器
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');
推荐答案
首先
return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
在执行compact
时,您已经在视图中包含了要用作$posts
的$posts
var,与使用with('posts', $posts)
所做的操作完全相同.因此,可以删除此最后一个.如果要使用compact
加载更多的变量,只需用逗号分隔变量名.
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.
您正在Search
的POST
操作中对项目进行分页,当您单击任何分页链接或调用page=2
时,您未在运行postSearch
方法,您可能正在调用getSearch
,因为您现在不执行POST
请求.
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.
看看 RESTful控制器
这篇关于Laravel 4中的分页仅占一页.但不为另一个工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!