Agent skill
resources
API resource classes that transform Eloquent models into JSON-ready arrays. Resources control exactly what is exposed in API responses and handle relationships, conditional attributes, and date formatting.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/resources-codebar-ag-coding-guidelines
SKILL.md
Name: Resources Description: API resource classes that transform Eloquent models into JSON-ready arrays. Resources control exactly what is exposed in API responses and handle relationships, conditional attributes, and date formatting. Compatible Agents: general-purpose, backend Tags: app/Http/Resources/**/*.php, laravel, php, backend, api, resource, json, response
Rules
- Resource classes live in
app/Http/Resources/ - Single resource:
ModelResource→UserResource,PostResource - Collection resource:
ModelCollection→UserCollection,PostCollection - Only expose what the API consumer needs — never blindly expose every model attribute
- Use
whenLoaded()for related resources — never eager load inside the resource itself - Use
when(),whenHas(), orwhenNotNull()for conditional attributes — notifstatements in the array - Use
mergeWhen()for multiple attributes sharing the same condition - Use
UserResource::collection()for simple collections — only create a dedicated collection class when custom meta is needed - Do not add pagination meta manually — pass a paginator directly and let Laravel append it
Examples
class UserResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'posts' => PostResource::collection($this->whenLoaded('posts')),
'secret' => $this->when($request->user()->isAdmin(), $this->secret),
'bio' => $this->whenNotNull($this->bio),
'created_at' => DateHelper::format($this->created_at),
];
}
}
// Controller — eager load before passing to resource
return UserResource::collection(User::with('posts')->paginate());
// Dedicated collection — only when custom meta is needed
class UserCollection extends ResourceCollection
{
public function toArray(Request $request): array
{
return [
'data' => $this->collection,
'links' => ['self' => route('users.index')],
];
}
}
Anti-Patterns
- Exposing all model attributes without whitelisting
- Eager loading relationships inside the resource's
toArray()— load them in the controller - Using
ifstatements inside the response array instead ofwhen(),whenLoaded(),whenNotNull() - Creating a dedicated collection class just for a simple list (use
UserResource::collection()) - Putting business logic or database queries in a resource
- Manually adding pagination meta that Laravel already provides automatically
References
- Laravel API Resources
- Related:
Controllers/SKILL.md— eager loading is done in the controller - Related:
Helpers/SKILL.md— useDateHelper::format()for date formatting
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?