Laravel paginate method not working with map collection?

后端 未结 2 574
北恋
北恋 2021-02-19 09:28
$items = Item::with(\'product\')->paginate(10);

    $items = $items->map(function($product){
          $product->name = \"Test\";
          return $product; 
          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 10:09

    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;
    });
    

    Also, since, paginator's items are a collection. You can use foreach directly on $items

    foreach ($items as $item)
    {
     $item->name = "Test";
    }
    

提交回复
热议问题