Laravel API resources for JSON transformation

Carlos Mendez Jan 2026
2 tabs
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'excerpt' => $this->excerpt,
            'body' => $this->when($request->user()?->id === $this->user_id, $this->body),
            'status' => $this->status,
            'published_at' => $this->published_at?->toISOString(),
            'created_at' => $this->created_at->toISOString(),
            'updated_at' => $this->updated_at->toISOString(),

            // Computed properties
            'read_time' => $this->readTime(),
            'is_published' => $this->isPublished(),

            // Conditional relationships
            'author' => new UserResource($this->whenLoaded('author')),
            'tags' => TagResource::collection($this->whenLoaded('tags')),
            'comments' => CommentResource::collection($this->whenLoaded('comments')),

            // Counts
            'comments_count' => $this->when(
                $this->relationLoaded('comments'),
                fn () => $this->comments->count()
            ),

            // Links
            'links' => [
                'self' => route('api.posts.show', $this->id),
                'author' => route('api.users.show', $this->user_id),
            ],
        ];
    }

    public function with(Request $request): array
    {
        return [
            'meta' => [
                'version' => '1.0',
            ],
        ];
    }
}
2 files · php Explain with highlit

API resources transform Eloquent models into JSON responses with full control over structure and data exposure. I create resource classes that extend JsonResource and define a toArray() method returning the desired JSON structure. Resources hide sensitive attributes, format dates, include computed properties, and conditionally include relationships. The whenLoaded() method includes relationships only if they're eager loaded, preventing N+1 queries. Resource collections handle arrays of models, while preserveQuery() maintains pagination links. Anonymous resources work for one-off transformations without creating dedicated classes. The with() method adds meta information to all responses. This layer provides consistent API responses and decouples database structure from API contracts.