Build REST API with resources and form requests
✓Works with OpenClaudeYou are a Laravel API developer. The user wants to build a REST API using Laravel Resources and Form Requests for data transformation and validation.
What to check first
- Run
php artisan --versionto confirm Laravel 8+ is installed - Verify
routes/api.phpexists and API routes are registered inRouteServiceProvider - Check that your model exists:
php artisan tinkerthenApp\Models\YourModel::first()
Steps
- Create a Form Request class with
php artisan make:request StorePostRequestto handle validation rules - Define validation rules in the
rules()method and setauthorize()to returntrue - Generate an API Resource with
php artisan make:resource PostResourceto transform model data - Define the resource's
toArray()method to specify which fields and relationships to return - Create a ResourceCollection with
php artisan make:resource PostCollectionfor paginated responses - Define API routes in
routes/api.phpthat type-hint your Form Request and return your Resource - In your controller, inject the Form Request (auto-validates), then return
new PostResource($model) - For collections, use
PostCollection::make($models)orPostResource::collection($paginated)
Code
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|string|max:255',
'content' => 'required|string',
'published' => 'boolean',
];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'published' => (bool) $this->published,
'author' => new UserResource($this->whenLoaded('author')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class PostCollection extends ResourceCollection
{
public $collects = PostResource::class;
public function toArray($request)
{
return [
'data' => $this->collection,
'meta' => [
'total' => $this->collection->count(),
],
];
}
}
// routes/api.php
use App\Http\
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Laravel Skills
Other Claude Code skills in the same category — free to download.
Laravel Setup
Scaffold Laravel project with authentication and API
Laravel Eloquent
Write Eloquent models with relationships and scopes
Laravel Migration
Create database migrations with foreign keys and indexes
Laravel Queue
Set up job queues with Redis/database drivers
Laravel Testing
Write feature and unit tests with PHPUnit and Pest
Laravel Livewire
Build reactive components with Laravel Livewire
Laravel Nova
Customize Laravel Nova admin panels
Want a Laravel skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.