Laravel分页方法不适用于地图收集?
问题描述:
$items = Item::with('product')->paginate(10);
$items = $items->map(function($product){
$product->name = "Test";
return $product;
});
return response()->json(['items' => $items]);
在控制台上的视图刀片中没有显示分页对象吗?
In my view blade on console paginate objects not showed?
答
如 Laravel docs ,Map()
方法创建一个新集合.要修改当前集合中的值,请使用transform()
方法.
As, given in Laravel docs, Map()
method creates a new collection. To modify the values in the current collection, use transform()
method.
$items->getCollection()->transform(function ($product) {
$product->name = "Test";
return $product;
});
此外,既然,分页者的项目是一个集合.您可以直接在$items
Also, since, paginator's items is a collection. You can use foreach
directly on $items
foreach ($items as $item)
{
$item->name = "Test";
}