@markdown# API Resources: With or Without "data"?If you use Eloquent API Resources to return data, they will be automatically wrapped in 'data'. If you want to remove it, add `JsonResource::withoutWrapping();` in `app/Providers/AppServiceProvider.php`.```class AppServiceProvider extends ServiceProvider{ public function boot() { JsonResource::withoutWrapping(); }}```Tip given by [@phillipmwaniki](https://twitter.com/phillipmwaniki/status/1445230637544321029)### API Return "Everything went ok"If you have API endpoint which performs some operations but has no response, so you wanna return just "everything went ok", you may return 204 status code "Nocontent". In Laravel, it's easy: `return response()->noContent();`.```phppublic function reorder(Request $request){ foreach ($request->input('rows', []) as $row) { Country::find($row['id'])->update(['position' => $row['position']]); } return response()->noContent();}```### Avoid N+1 queries in API resourcesYou can avoid N+1 queries in API resources by using the `whenLoaded()` method.
This will only append the department if it’s already loaded in the Employee model.
Without `whenLoaded()` there is always a query for the department```phpclass EmplyeeResource extends JsonResource{ public function toArray($request): array { return [ 'id' => $this->uuid, 'fullName' => $this->full_name, 'email' => $this->email, 'jobTitle' => $this->job_title, 'department' => DepartmentResource::make($this->whenLoaded('department')), ]; }}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1473987501501071362)### Get Bearer Token from Authorization headerThe `bearerToken()` function is very handy when you are working with apis & want to access the token from Authorization header.```php// Don't parse API headers manually like this:$tokenWithBearer = $request->header('Authorization');$token = substr($tokenWithBearer, 7);//Do this instead:$token = $request->bearerToken();```Tip given by [@iamharis010](https://twitter.com/iamharis010/status/1488413755826327553)### Sorting Your API ResultsSingle-column API sorting, with direction control```php// Handles /dogs?sort=name and /dogs?sort=-nameRoute::get('dogs', function (Request $request) { // Get the sort query parameter (or fall back to default sort "name") $sortColumn = $request->input('sort', 'name'); // Set the sort direction based on whether the key starts with - // using Laravel's Str::startsWith() helper function $sortDirection = Str::startsWith($sortColumn, '-') ? 'desc' : 'asc'; $sortColumn = ltrim($sortColumn, '-'); return Dog::orderBy($sortColumn, $sortDirection) ->paginate(20);});```we do the same for multiple columns (e.g., ?sort=name,-weight)```php// Handles ?sort=name,-weightRoute::get('dogs', function (Request $request) { // Grab the query parameter and turn it into an array exploded by , $sorts = explode(',', $request->input('sort', '')); // Create a query $query = Dog::query(); // Add the sorts one by one foreach ($sorts as $sortColumn) { $sortDirection = Str::startsWith($sortColumn, '-') ? 'desc' : 'asc'; $sortColumn = ltrim($sortColumn, '-'); $query->orderBy($sortColumn, $sortDirection); } // Return return $query->paginate(20);});```@endmarkdown
This will only append the department if it’s already loaded in the Employee model.
Without `whenLoaded()` there is always a query for the department```phpclass EmplyeeResource extends JsonResource{ public function toArray($request): array { return [ 'id' => $this->uuid, 'fullName' => $this->full_name, 'email' => $this->email, 'jobTitle' => $this->job_title, 'department' => DepartmentResource::make($this->whenLoaded('department')), ]; }}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1473987501501071362)### Get Bearer Token from Authorization headerThe `bearerToken()` function is very handy when you are working with apis & want to access the token from Authorization header.```php// Don't parse API headers manually like this:$tokenWithBearer = $request->header('Authorization');$token = substr($tokenWithBearer, 7);//Do this instead:$token = $request->bearerToken();```Tip given by [@iamharis010](https://twitter.com/iamharis010/status/1488413755826327553)### Sorting Your API ResultsSingle-column API sorting, with direction control```php// Handles /dogs?sort=name and /dogs?sort=-nameRoute::get('dogs', function (Request $request) { // Get the sort query parameter (or fall back to default sort "name") $sortColumn = $request->input('sort', 'name'); // Set the sort direction based on whether the key starts with - // using Laravel's Str::startsWith() helper function $sortDirection = Str::startsWith($sortColumn, '-') ? 'desc' : 'asc'; $sortColumn = ltrim($sortColumn, '-'); return Dog::orderBy($sortColumn, $sortDirection) ->paginate(20);});```we do the same for multiple columns (e.g., ?sort=name,-weight)```php// Handles ?sort=name,-weightRoute::get('dogs', function (Request $request) { // Grab the query parameter and turn it into an array exploded by , $sorts = explode(',', $request->input('sort', '')); // Create a query $query = Dog::query(); // Add the sorts one by one foreach ($sorts as $sortColumn) { $sortDirection = Str::startsWith($sortColumn, '-') ? 'desc' : 'asc'; $sortColumn = ltrim($sortColumn, '-'); $query->orderBy($sortColumn, $sortDirection); } // Return return $query->paginate(20);});```@endmarkdown