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.

Stars 163
Forks 31

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

bash
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

Expand your agent's capabilities with these related and highly-rated skills.

Didn't find tool you were looking for?

Be as detailed as possible for better results