$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
LaravelintermediateNew

Laravel API Resource

Share

Build REST API with resources and form requests

Works with OpenClaude

You 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 --version to confirm Laravel 8+ is installed
  • Verify routes/api.php exists and API routes are registered in RouteServiceProvider
  • Check that your model exists: php artisan tinker then App\Models\YourModel::first()

Steps

  1. Create a Form Request class with php artisan make:request StorePostRequest to handle validation rules
  2. Define validation rules in the rules() method and set authorize() to return true
  3. Generate an API Resource with php artisan make:resource PostResource to transform model data
  4. Define the resource's toArray() method to specify which fields and relationships to return
  5. Create a ResourceCollection with php artisan make:resource PostCollection for paginated responses
  6. Define API routes in routes/api.php that type-hint your Form Request and return your Resource
  7. In your controller, inject the Form Request (auto-validates), then return new PostResource($model)
  8. For collections, use PostCollection::make($models) or PostResource::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

Quick Info

CategoryLaravel
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
laravelapirest

Install command:

curl -o ~/.claude/skills/laravel-api-resource.md https://clskills.in/skills/laravel/laravel-api-resource.md

Related Laravel Skills

Other Claude Code skills in the same category — free to download.

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.