Laravel 4 分页集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15233667/
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 4 paginate collection
提问by user2137406
I cant create a proper pagination system using laravel 4. I have the following models and function that return collections:
我无法使用 laravel 4 创建合适的分页系统。我有以下返回集合的模型和函数:
Model Restaurant:
示范餐厅:
public function fooditem()
{
return $this->hasMany('Fooditem','rest_id');
}
public function get_rest_foods($id){
return Restaurant::find($id)->fooditem->toArray();
}
The second function returns all food items for a certain restaurant as an array. I also use this in an API call.
第二个函数以数组的形式返回某个餐厅的所有食品。我也在 API 调用中使用它。
in my controller i have this:
在我的控制器中,我有这个:
$food = $food->get_rest_foods($id);
$paginator = Paginator::make($food, 10, 5);
I pass the paginator to the view and it shows the links ok but also shows all my item from the food array.
我将分页器传递给视图,它显示链接正常,但也显示了食物数组中的所有项目。
I tried using
我尝试使用
public function get_rest_foods($id){
return Restaurant::find($id)->fooditem->paginate(5);
}
but i get an error:
但我收到一个错误:
FatalErrorException: Error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
FatalErrorException:错误:调用未定义的方法 Illuminate\Database\Eloquent\Collection::paginate()
I searched this and many other sites but cant understant how to paginate a collection.
我搜索了这个网站和许多其他网站,但无法理解如何对集合进行分页。
Thanks for your help
谢谢你的帮助
回答by blablabla
The paginator must get the items that it would normally get from a database query with an offset/limit statement. So when you have a collection with all items, you should do the offset/limit yourself.
分页器必须使用偏移/限制语句获取它通常从数据库查询中获取的项目。因此,当您拥有包含所有项目的集合时,您应该自己进行偏移/限制。
$food = $food->get_rest_foods($id);
$page = 1;
if( !empty(Input::get['page']) ) {
$page = Input::get['page'];
}
$perPage = 15;
$offset = (($page - 1) * $perPage);
$food = Paginator::make($food->slice($offset,$perPage, true)->all(), $food->count(), $perPage);
回答by Zayin Krige
I created a subclass of Collection and implemented my own paginate method
我创建了一个 Collection 的子类并实现了我自己的 paginate 方法
public function paginate($perPage) {
$pagination = App::make('paginator');
$count = $this->count();
$page = $pagination->getCurrentPage($count);
$items = $this->slice(($page - 1) * $perPage, $perPage)->all();
$pagination = $pagination->make($items, $count, $perPage);
return $pagination;
}
回答by Jason Lewis
The Laravel paginator does not do any of the splicing for you. Typically the paginator should be used hand in hand with Eloquent/Fluent. In your second example you should be doing it like this.
Laravel 分页器不会为您做任何拼接。通常,分页器应该与 Eloquent/Fluent 一起使用。在您的第二个示例中,您应该这样做。
return Restaurant::find($id)->fooditem()->paginate(5);
Without calling the actual method you'll just be getting a collection of results and not the query builder instance.
在不调用实际方法的情况下,您只会获得结果集合,而不是查询构建器实例。
If you want to use the paginator manually you need to pass in the spliced array (the correct items for the current page). This usually involves determining the current page, the total results and total pages. That's why, where possibly, it's best to use it with Eloquent or Fluent.
如果要手动使用分页器,则需要传入拼接数组(当前页面的正确项)。这通常涉及确定当前页面、总结果和总页面。这就是为什么在可能的情况下,最好将它与 Eloquent 或 Fluent 一起使用。