Laravel 更改分页数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37102841/
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 change pagination data
提问by TheUnreal
My Laravel pagination output is like laravel pagination used to be, but I need to change the data array for each object. My output is:
我的 Laravel 分页输出就像以前的 Laravel 分页一样,但我需要更改每个对象的数据数组。我的输出是:
As you can see, the data object has 2 items, which I need to change.
如您所见,数据对象有 2 个项目,我需要对其进行更改。
My code is:
我的代码是:
$items = $this->items()
->where('position', '=', null)
->paginate(15);
Which returns the user items with pivot table, but I don't like the way the pivot table is shown in the JSON, so I decided to change the items and organize each item with the pivot before the item.
它返回带有数据透视表的用户项目,但我不喜欢数据透视表在 JSON 中的显示方式,因此我决定更改项目并使用项目之前的数据透视表组织每个项目。
For this purpose, I tried to use foreach
为此,我尝试使用 foreach
foreach ($items->data as $item)
{
}
which giving my an error, for a reason I don't know:
这给了我一个错误,原因我不知道:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$data"
status_code: 500
Any help?
有什么帮助吗?
回答by Murwa
The paginator's items is a collection. You can grab it and transform the data like so:
分页器的项目是一个集合。您可以抓取它并像这样转换数据:
$paginator = $this->items()->where('position', '=', null)->paginate(15);
$paginator->getCollection()->transform(function ($value) {
// Your code here
return $value;
});
回答by hellovoid
If you'd like to keep items paginated:
如果您想保持项目分页:
$itemsPaginated = $this->items()
->paginate(15);
$itemsTransformed = $itemsPaginated
->getCollection()
->map(function($item) {
return [
'id' => $item->id,
];
})->toArray();
$itemsTransformedAndPaginated = new \Illuminate\Pagination\LengthAwarePaginator(
$itemsTransformed,
$itemsPaginated->total(),
$itemsPaginated->perPage(),
$itemsPaginated->currentPage(), [
'path' => \Request::url(),
'query' => [
'page' => $itemsPaginated->currentPage()
]
]
);
回答by Scofield
There is a setCollection
method for such purpose.
有一种setCollection
方法可以达到这种目的。
$items = Model::paginate(10);
$updatedItems = $items->getCollection();
// data manipulation
// ...
$items->setCollection($updateItems);
From the source code of /Illuminate/Pagination/AbstractPaginator.php
从源代码 /Illuminate/Pagination/AbstractPaginator.php
/**
* Set the paginator's underlying collection.
*
* @param \Illuminate\Support\Collection $collection
* @return $this
*/
public function setCollection(Collection $collection)
{
$this->items = $collection;
return $this;
}
回答by umihico
I could make shorter way. This returns edited $array
instead of simple $paginated
. This example modify file names.
This docwas useful for me.
我可以缩短路线。这将返回edited$array
而不是 simple $paginated
。本示例修改文件名。这个文档对我很有用。
$paginated=$query->paginate(12);
$array=$paginated->toArray();
foreach ($array['data'] as $r=>$record) {
$array['data'][$r]->gif=$array['data'][$r]->gif.".gif";
}
return $array;
回答by Dzung Nguyen
-Laravel 5.4
-Laravel 5.4
// example update column "photo"
// from "/path/to/photo.png"
// to "abc.com/path/to/photo.png"
foreach ($items as $item)
{
$path = $item->photo;
// Remove
$item->offsetUnset("photo");
// Set
$item->offsetSet("photo", url($path));
}
回答by Christian Ndukwe
<?php
$itemsPaginated = $this->items()->paginate(15);
$itemsPaginated = json_encode($itemsPaginated);
foreach ($itemsPaginated->data as $key => $item) {
$results->data[$key]; //Modify
}
$itemsPaginated = json_encode($results);
回答by hamedkke
Ignore the pagination in laravel and hit the normal data
忽略laravel中的分页,命中正常数据
foreach ($items as $item)
{
}
回答by Veerendra Borra
you have to use below code in your blade
您必须在刀片中使用以下代码
{!! $items->render() !!}