Laravel Tutorials

Using withoutWrapping to flatten API responses

Published Updated
Using withoutWrapping to flatten API responses image

Laravel's API resources wrap responses in a 'data' key by default. While useful for many scenarios, sometimes a flatter response structure is needed and you can disable resource wrapping like this.

<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        JsonResource::withoutWrapping();
    }
}

Here is an example of how this works when you have withoutWrapping:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ArticleResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'content' => $this->content,
            'author' => new AuthorResource($this->whenLoaded('author')),
            'metadata' => [
                'views' => $this->views_count,
                'likes' => $this->likes_count,
                'published_at' => $this->published_at
            ]
        ];
    }
}

Example response with wrapping disabled:

[
    {
        "id": 1,
        "title": "Laravel Tips",
        "content": "Article content here",
        "author": {
            "id": 1,
            "name": "John Doe",
            "email": "john@example.com"
        },
        "metadata": {
            "views": 150,
            "likes": 42,
            "published_at": "2024-03-15T10:00:00Z"
        }
    }
]

This implementation provides a cleaner API structure while maintaining the flexibility to customize response formats based on your application's needs.

Resource wrapping can be disabled globally while still maintaining granular control over your API's response structure, resulting in more intuitive and easier-to-consume endpoints.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article
Pollora: Run WordPress Inside a Laravel Application image

Pollora: Run WordPress Inside a Laravel Application

Read article
Scheduled Session Garbage Collection in Laravel 13.39 image

Scheduled Session Garbage Collection in Laravel 13.39

Read article