Support the ongoing development of Laravel.io →
posted 3 weeks ago
API
Last updated 2 weeks ago.
0
Solution

Yeah, that JSON structure you're using is pretty heavy. JSON:API solves some problems, but for most Laravel apps it just adds a lot of noise. If you don’t really need JSON:API compatibility, you can go with something much cleaner and easier to work with. A common pattern is:

  • Keep objects mostly flat
  • Nest relationships only when needed
  • Maybe wrap everything in a simple "data" key
  • Skip the whole type, attributes, relationships, included ceremony.
// Example list response:
{
  "data": [
    {
      "id": 1,
      "foo": "bar"
    }
  ]
}

// Example show response with an included author:
{
  "data": {
    "id": 1,
    "title": "Bug in checkout",
    "description": "Steps to reproduce...",
    "status": "open",
    "author": {
      "id": 5,
      "name": "Jeremy",
      "email": "jeremy@example.com"
    }
  }
}
// Nice and clean, no extra layers.

A simpler TicketResource example:

public function toArray(Request $request): array
{
    return [
        'id'     => $this->id,
        'title'  => $this->title,
        'description' => $this->when(
            $request->routeIs('tickets.*'),
            $this->description
        ),
        'status' => $this->status,
        // Include relationship only if it's loaded
        'author' => new UserResource($this->whenLoaded('author')),
        // Optional links if you like that style
        'links' => [
            'self'   => route('tickets.show', $this->id),
            'author' => $this->when(
                $this->relationLoaded('author'),
                fn () => route('authors.show', $this->author->id)
            ),
        ],
    ];
}

// Controller example
$tickets = Ticket::query()
    ->when($request->boolean('include_author'), fn ($q) => $q->with('author'))
    ->paginate();

return TicketResource::collection($tickets);

There’s no official Laravel standard for API shape but this lighter style is extremely common and much easier to maintain. If you don’t need strict JSON:API rules, going simpler is usually the better

1
Solution selected by @mina20088

Laravel API Resources provide a simple, consistent way to transform models into clean JSON responses without heavy standards like JSON:API. They let you control response structure while keeping create/update payloads straightforward.

https://laravel.com/docs/10.x/eloquent-resources

0

Laravel API Resources offer an easy and consistent way to convert models into clean JSON responses without relying on complex standards like JSON:API. They give you full control over how responses are structured while keeping create and update requests simple and easy to manage. https://geometrydeshapks.com/

0

Sign in to participate in this thread!

Native PHP

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2026 Laravel.io - All rights reserved.