A library that will help you format the response easily
composer require hoangdo/laravel-json-api
// web.php
Route::get('foo', 'FooController@index')->middleware('json');
// or
Route::middleware('json')->group(function () {
Route::get('foo', 'FooController@index');
Route::get('bar', 'BarController@index');
})
// app/Http/Kernel.php
...
protected $middlewareGroups = [
...
'api' => [
// Add it here
'json',
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
...
];
...
If you want to change the middleware alias name, for avoid conflict with another libraries, just fix it by .env
# You can add multiple aliases, separated by ","
JSON_MIDDLEWARE_NAME=json1,json2