Question:
How to disable data wrapping in API Resources in Laravel 7 ?
Solution:
You can disable data wrapping by calling the withoutWrapping
static method of your resource in the AppServiceProvider
.
Example How to disable data wrapping in all API Resources:
<?php
namespace App\Providers;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
JsonResource::withoutWrapping();
}
}
Example How to disable data wrapping in specific API Resource:
public function boot()
{
UserResource::withoutWrapping();
}