@mastermarco To add additional data to the resource, you have to do add the $additional property to the resource.
class CategoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'recipes_count' => $this->recipes_count,
];
}
public $additional = [
'new_data' => 'new_value',
];
}
https://laravel.com/api/10.x/Illuminate/Http/Resources/Json/JsonResource.html
What I don't understand is that even the additional data is added to the response :
dd(CategoryResource::collection($categories)); // the additional data is displayed
return CategoryResource::collection($categories); // the additional data isn't displayed
Furthermore I don't know if it's a good idea to add additional data directly by assigning a value to this property.