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

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article