Skip to content
Back to skills

Laravel Api Resources

ASecurity

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.

  • 30 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 12, 2026
ai-agentsphpbashapidocumentation

Works with

  • api

Security analysis

A100/100

Scanned November 12, 2026

npx -y skills add majiayu000/claude-skill-registry-data --skill laravel-api-resources --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Laravel Api Resources?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Laravel Api Resources
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/majiayu000-laravel-api-resources-claude-skill-registry-data/badge)](https://www.skillsdirectory.com/skills/majiayu000-laravel-api-resources-claude-skill-registry-data)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: laravel-api-resources
description: >-
  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.
---

# 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

<code-snippet name="Basic Resource" lang="php">
<?php

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(),
        ];
    }
}
</code-snippet>

### Resource Collections

<code-snippet name="Resource Collection" lang="php">
<?php

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(),
            ],
        ];
    }
}
</code-snippet>

### Conditional Attributes

<code-snippet name="Conditional Attributes" lang="php">
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,

        // 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),
    ];
}
</code-snippet>

### Nested Resources

<code-snippet name="Nested Resources" lang="php">
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'author' => new UserResource($this->whenLoaded('author')),
        'comments' => CommentResource::collection($this->whenLoaded('comments')),
    ];
}
</code-snippet>

### Using Resources in Controllers

<code-snippet name="API Controller" lang="php">
<?php

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);
    }
}
</code-snippet>

### API Routes

<code-snippet name="API Routes" lang="php">
// routes/api.php
use App\Http\Controllers\Api\UserController;

Route::prefix('v1')->group(function () {
    Route::apiResource('users', UserController::class);
});
</code-snippet>

### API Versioning

<code-snippet name="API Versioning Structure" lang="text">
app/
├── Http/
│   ├── Controllers/
│   │   └── Api/
│   │       ├── V1/
│   │       │   └── UserController.php
│   │       └── V2/
│   │           └── UserController.php
│   └── Resources/
│       ├── V1/
│       │   └── UserResource.php
│       └── V2/
│           └── UserResource.php
</code-snippet>

### Response Wrapping

<code-snippet name="Custom Response Wrapper" lang="php">
// In AppServiceProvider boot()
JsonResource::withoutWrapping();

// Or customize the wrapper
public static $wrap = 'user';
</code-snippet>

### Pagination

<code-snippet name="Paginated Response" lang="php">
public function index()
{
    $users = User::paginate(15);

    // Automatically includes pagination meta
    return UserResource::collection($users);
}

// Response structure:
// {
//     "data": [...],
//     "links": { "first": "...", "last": "...", ... },
//     "meta": { "current_page": 1, "total": 100, ... }
// }
</code-snippet>

### Additional Meta Data

<code-snippet name="Additional Meta" lang="php">
return (new UserCollection($users))
    ->additional([
        'meta' => [
            'version' => 'v1',
            'generated_at' => now()->toISOString(),
        ],
    ]);
</code-snippet>

## 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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.