laravel-api-resource-patterns
Learn how to structure Laravel API Resources for clean model transformation and consistent JSON responses. This skill covers conditional attributes, relationship loading, collections with pagination, and techniques to prevent N+1 queries.
Laravel API Resource Patterns teaches best practices for transforming Eloquent models into structured JSON responses.
AI-generated summary based on this skill's SKILL.md
Install
iSerter/laravel-claude-agents/laravel-api-resource-patterns · repository language: PHP
git clone https://github.com/iSerter/laravel-claude-agents
cp -r laravel-claude-agents/skills/laravel-api-resource-patterns ~/.claude/skills/laravel-api-resource-patternsnpx skillfed install iSerter/laravel-claude-agents/laravel-api-resource-patternsFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What are laravel api resource best practices?
laravel-api-resource-patterns teaches you to structure API Resources for clean, consistent JSON responses. Best practices include using whenLoaded() to prevent N+1 queries, defining conditional attributes for flexible responses, properly loading relationships before transformation, and organizing collections with pagination metadata. Resources act as a transformation layer between your models and API output.
How do you prevent N+1 queries using whenLoaded in resources?
laravel-api-resource-patterns covers using whenLoaded() to safely include relationship data only when it's already loaded on the model. This prevents the resource from triggering additional queries. Load relationships eagerly in your controller (e.g., User::with('posts')->get()), then in your Resource class use $this->whenLoaded('posts') to conditionally include them without extra database hits.
How can laravel resources transform models to JSON with conditional attributes?
laravel-api-resource-patterns explains conditional attribute inclusion through methods like when() and whenLoaded(). Define fields that appear only under certain conditions—for example, showing admin-only fields only to authenticated admins, or including timestamps only when requested. This keeps your API responses flexible and secure without duplicating resource classes.
How do you handle pagination and relationships in laravel resource collections?
laravel-api-resource-patterns demonstrates wrapping paginated results in ResourceCollections, which preserve pagination metadata (current_page, total, etc.) alongside your transformed data. Load relationships before passing to the collection, use whenLoaded() for optional includes, and leverage the collection's meta() method to attach custom metadata like links or filtering info.
What is the purpose of laravel resource wrapping and unwrapping?
laravel-api-resource-patterns covers resource wrapping—automatically nesting your data under a key (e.g., 'data')—and unwrapping to return raw arrays. Wrapping provides consistent envelope structure for collections and error responses; unwrapping simplifies single-resource endpoints. Control this via config or the resource's wrap() method to match your API design.
How do you structure RESTful API responses with links and metadata?
laravel-api-resource-patterns shows adding HATEOAS links and metadata to resources via the with() method and custom toArray() logic. Include self links, related resource links, and pagination links in collections. Attach metadata like total counts, filtering info, or API version. This makes your API self-documenting and easier for clients to navigate.
SKILL.md
rendered from the published skill — quoted content, verbatim
API Resource Patterns
Basic Resource Structure
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'created_at' => $this->created_at->toISOString(),
'updated_at' => $this->updated_at->toISOString(),
];
}
}
Conditional Attributes
```php public function toArray($request): array { return [ 'id' => $this->id, 'title' => $this->title,
// Only include if loaded
'author' => new UserResource($this->whenLoaded('user')),
// Only include if condition is true
'content' => $this->when($request->user()?->can('view', $this->resource), $this->content),
// Only include if not null
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/laravel-api-resource-patterns/SKILL.md