Laravel API Resources Transformation
January 16, 2026
•
1 min read
•
13 views
Table of Contents
API resources:
Create Resource
php artisan make:resource UserResource
Resource Class
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'posts' => PostResource::collection($this->whenLoaded('posts')),
];
}
}
Use in Controller
return new UserResource($user);
return UserResource::collection($users);
Additional Data
return (new UserResource($user))->additional([
'meta' => ['version' => '1.0'],
]);
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!