bogdy's avatar
Level 10

How to Change the Default JSON Resource Wrapper in Laravel 11?

I'm working on an API using Laravel 11 and I want to change the default JSON wrapper for my resources. In previous versions of Laravel, I used to set a static property $wrap in the resource class to achieve this, like so:

 public static $wrap = 'test';

but now dont work more

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel 11, the approach to changing the default JSON resource wrapper has been updated. You can still set the $wrap property, but the way you do it might have changed slightly. Here’s how you can achieve this in Laravel 11:

  1. Setting the $wrap Property in the Resource Class:

    You can still set the $wrap property in your resource class to change the default wrapper. Here’s an example:

    <?php
    
    namespace App\Http\Resources;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class YourResource extends JsonResource
    {
        /**
         * The "data" wrapper that should be applied.
         *
         * @var string|null
         */
        public static $wrap = 'test';
    
        /**
         * Transform the resource into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'name' => $this->name,
                // Add other fields here
            ];
        }
    }
    
  2. Disabling the Wrapper Globally:

    If you want to disable the wrapper globally for all resources, you can do so in the AppServiceProvider or any other service provider by using the withoutWrapping method:

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            JsonResource::withoutWrapping();
        }
    }
    
  3. Customizing the Wrapper Dynamically:

    If you need to customize the wrapper dynamically, you can override the with method in your resource class:

    <?php
    
    namespace App\Http\Resources;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class YourResource extends JsonResource
    {
        /**
         * Transform the resource into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'name' => $this->name,
                // Add other fields here
            ];
        }
    
        /**
         * Customize the additional data that should be included with the resource array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function with($request)
        {
            return [
                'meta' => [
                    'custom' => 'value',
                ],
            ];
        }
    }
    

By following these steps, you should be able to change the default JSON resource wrapper in Laravel 11. If the static $wrap property is not working as expected, ensure that you are using the correct namespace and that there are no typos in your code.

bogdy's avatar
Level 10

I solved like this: Instead of setting the static $wrap property, I modified the controller to manually wrap the resource collection in the desired key. Here is the updated code:

return [ 'test' => ProductResource::collection($products), // Wrap the collection ];

Please or to participate in this conversation.