Skip to content
Advertisement

pass additional data to laravel resource

i need to pass My custom data to the same level of array of resource but it goes out of array

return CategoryProductsResource::collection(Category::whereNull('parent_id')->get())
  ->additional([
                'data' => [
                    'id' => 9999,
                    'name' => 'How Offers',
                    'image' => 'http://businessdotkom.com/storage/categories/January2020/1o6nDi1kjVuwje5FiFXv.png',
                    'products' => ProductIndexResource::collection(Product::whereNotNull('sale_price')->get()),
                ]
            ]);

json output

enter image description here

Advertisement

Answer

Because additional is for top-level data, use concat instaed.

return CategoryProductsResource::collection(Category::whereNull('parent_id')->get())
  ->concat([
                'data' => [
                    'id' => 9999,
                    'name' => 'How Offers',
                    'image' => 'http://businessdotkom.com/storage/categories/January2020/1o6nDi1kjVuwje5FiFXv.png',
                    'products' => ProductIndexResource::collection(Product::whereNotNull('sale_price')->get()),
                ]
            ]);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement