Agent skill
laravel-api-resources
Develops API endpoints with Eloquent resources and versioning. Activates when creating API routes, Eloquent resources, resource collections, API controllers, or when user mentions API, resource, JSON response, REST, endpoint, or API versioning.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/laravel-api-resources
SKILL.md
Laravel API Resources Development
When to Apply
Activate this skill when:
- Creating API endpoints and routes
- Building Eloquent API Resources
- Implementing resource collections
- Designing API response structures
- Working with API versioning
Documentation
Use search-docs for detailed Laravel API Resource patterns and documentation.
Basic Usage
Creating Resources
php artisan make:resource UserResource
php artisan make:resource UserCollection
Basic Resource
namespace App\Http\Resources;
use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource { public function toArray(Request $request): array { return [ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, 'created_at' => $this->created_at->toISOString(), ]; } }
Resource Collections
namespace App\Http\Resources;
use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\ResourceCollection;
class UserCollection extends ResourceCollection { public function toArray(Request $request): array { return [ 'data' => $this->collection, 'meta' => [ 'total' => $this->collection->count(), ], ]; } }
Conditional Attributes
// Include only when loaded
'posts' => PostResource::collection($this->whenLoaded('posts')),
// Include conditionally
'secret' => $this->when($request->user()?->isAdmin(), $this->secret),
// Include when not null
'bio' => $this->whenNotNull($this->bio),
];
}
Nested Resources
Using Resources in Controllers
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Resources\UserResource; use App\Http\Resources\UserCollection; use App\Models\User;
class UserController extends Controller { public function index() { $users = User::with('posts')->paginate(15);
return new UserCollection($users);
}
public function show(User $user)
{
$user->load('posts', 'profile');
return new UserResource($user);
}
}
API Routes
Route::prefix('v1')->group(function () { Route::apiResource('users', UserController::class); });
API Versioning
Response Wrapping
// Or customize the wrapper public static $wrap = 'user';
Pagination
// Automatically includes pagination meta
return UserResource::collection($users);
}
// Response structure: // { // "data": [...], // "links": { "first": "...", "last": "...", ... }, // "meta": { "current_page": 1, "total": 100, ... } // }
Additional Meta Data
Common Pitfalls
- Not using
whenLoaded()for relationships (causes N+1 or null errors) - Exposing sensitive data in resources (passwords, tokens)
- Not eager loading relationships before passing to resources
- Missing API versioning from the start
- Not using pagination for large collections
- Inconsistent response structures across endpoints
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?