3

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);
    }
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
Divyang
  • 372
  • 1
  • 5
  • 21
  • 1
    Resource collection is a collection so `$this->name` will not exist. Perhaps you meant `$this->collection->first()->name` or something like that – apokryfos Oct 21 '18 at 08:47
  • 1
    see this https://stackoverflow.com/questions/46388205/laravel-5-5-api-resources-for-collections-standalone-data – Sohel0415 Oct 21 '18 at 08:52

3 Answers3

3

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;
    }
}
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
2

You can return a collection using a resource without looping by using the "collection" static method:

public function index()
    {
        return ProductCollection::collection(Product::all());
    }
NicuVlad
  • 2,491
  • 3
  • 28
  • 47
0

What I found was you never have to loop the collection if you want it.

In my case, I have an event collection. What I did was this.

php artisan make:resource Event

php artisan make:resource EventCollection

In the Event

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

and in the EventCollection just leave it as is:

return parent::toArray($request);