In laravel we can format our json response from resource class as seen below
class ProductsResource extends JsonResource
{
public function toArray($request)
{
return [
'id'=> $this->product_id ,
'code'=> $this->product_code,
'shortdescription'=> $this->product_short_description,
'image'=> $this->product_image,
];
}
}
But when returning resources collection i can't format my collection error Property [product_id] does not exist on this collection instance
class ProductsResource extends ResourceCollection
{
public function toArray($request)
{
return [
'id'=> $this->product_id ,
'code'=> $this->product_code,
'shortdescription'=> $this->product_short_description,
'image'=> $this->product_image,
];
}
}
thanks.
It's because
ResourceCollection
expects acollection
of items instead of asingle item
. The collection resource expects you to iterate through the collection and cannot perform single enitity routines directly from$this
(as it's a collection).See resource collections in documentation
What you are probably looking for is to cast a custom mutation which examples can be found here:
See custom casts in documentation
Look/search for
Value Object Casting
. It is explained thoroughly how to mutate attributes onget
andset
, which is probably better than a resource collection (if this is the only thing you wish to do with it). This will modify the collection immediately and saves you from having to manually instantiate the resource collection every time you need it (as you are modifying at model level).Coming from the docs:
But to get back on topic...
If you dump and die:
dd($this);
you will see there is an attribute called+collection
In case you wish to transform keys or values, you have to iterate through
$this->collection
in order to transform the collectionvalues
orkeys
to your requirements.As you can see in the parent class
Illuminate\Http\Resources\Json\ResourceCollection
the methodtoArray()
is already a mapped collection.In which you can see it's pointing to
$this->collection
You could use something like the following. And update the item/key values within this collection map.
if you wish to transform the values before returning it to an array.
Or a simple foreach like this (have not tested it and there are far better ways of doing this) But for the sake of sharing a
simple-to-grasp
example: