簡體   English   中英

Laravel:此集合實例上不存在屬性 [名稱]

[英]Laravel: Property [name] does not exist on this collection instance

我正在嘗試使用 Laravel 的資源集合轉換所有產品的 json 數據。 但它正在拋出錯誤。

“此集合實例上不存在屬性 [名稱]”。

我查看了官方文檔,他們以類似的方式實現了它。

產品控制器.php

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

產品系列.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
        ];
    }
}

產品資源.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)
            ]
        ];
    }
}

注意:在不轉換 ProductCollection 時工作正常,即當 ProductCollection 的toArray()函數如下所示時:

public function toArray($request)
    {
        return parent::toArray($request);
    }

ProductCollection用於Products的集合。
所以你需要使用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;
    }
}

您可以使用“集合”靜態方法使用資源返回集合而無需循環:

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

我發現如果你想要它,你永遠不必循環這個集合。

就我而言,我有一個事件集合。 我做的就是這個。

php artisan make:resource Event

php artisan make:resource EventCollection

在活動中

return [
  "id"        => $this->id,
  "title"     => $this->title,
  "parent_id" => $this->parent_id,
  "color"     => $this->color,
  "start"     => $this->start_date,
  "end"       => $this->end_date
];

並在 EventCollection 中保持原樣:

return parent::toArray($request);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM