douyasihefu6214 2018-10-21 08:23
浏览 100
已采纳

Laravel:此集合实例上不存在Property [name]

I am trying to transform json data for all products using Laravel's resource collections. But it is throwing error.

"Property [name] does not exist on this collection instance".

I checked official documentation, and they have implemented it in a similar way.

ProductController.php

public function index()
    {
        return new ProductCollection(Product::all());
    }

ProductCollection.php

namespace App\Http\Resources\Product;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ProductCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'price' => $this->price
        ];
    }
}

ProductResource.php

namespace App\Http\Resources\Product;

use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'description' => $this->detail,
            'price' => $this->price,
            'stock' => $this->stock,
            'discount' => $this->discount,
            'effectivePrice' => round($this->price * (1 - ($this->discount/100)), 2),
            'rating' => $this->reviews->count() > 0 ? round($this->reviews->sum('star') / $this->reviews->count(), 2) : 'No Ratigs Yet',
            'href' => [
                'reviews' => route('reviews.index', $this->id)
            ]
        ];
    }
}

NOTE: Working fine when not transforming the ProductCollection, i.e., when ProductCollection's toArray() function is like below:

public function toArray($request)
    {
        return parent::toArray($request);
    }
  • 写回答

1条回答 默认 最新

  • douyi6960 2018-10-21 09:14
    关注

    ProductCollection is for a collection of Products.
    So you need to use foreach.

    class ProductCollection extends ResourceCollection
    {
        /**
         * Transform the resource collection into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
    
            // final array to be return.
            $products = [];
    
            foreach($this->collection as $product) {
    
                 array_push($products, [
                     'name' => $product->name,
                     'price' => $product->price
                 ]);
    
            }
    
            return $products;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)