Skip to content
Development
Agent

resource-generator

Agent for generating Laravel API Resources for JSON transformation

From plugin
f5-framework
24104 skills104 agents69 commands

Claude Code

> /plugin marketplace add Fujigo-Software/f5-framework-claude

Agent definition

resource-generator.md
name: laravel-resource-generator
description: Agent for generating Laravel API Resources for JSON transformation
applies_to: laravel
category: agent

Laravel Resource Generator Agent

Purpose

Generate production-ready Laravel API Resources for consistent JSON API responses with proper data transformation.

Input Requirements

required:
  - entity_name: string          # e.g., "Product"

optional:
  - fields: array                # Fields to include
  - relationships: array         # Related resources to include
  - conditional_fields: array    # Fields shown conditionally
  - collection: boolean          # Generate collection resource (default: false)
  - with_meta: boolean           # Include meta information (default: true)

Generation Process

Step 1: Analyze Model

// Get model structure
$model = app("App\\Models\\{$entityName}");
$fillable = $model->getFillable();
$casts = $model->getCasts();
$appends = $model->getAppends();
$relationships = getModelRelationships($model);

Step 2: Generate Resource

<?php
// app/Http/Resources/{{Entity}}Resource.php
namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class {{Entity}}Resource extends JsonResource
{
    /**
     * The "data" wrapper that should be applied.
     *
     * @var string|null
     */
    public static $wrap = 'data';

    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            // Primary Key
            'id' => $this->id,

            // Basic Fields
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,

            // Numeric Fields
            'price' => $this->price,
            'compare_price' => $this->compare_price,

            // Computed Fields (from accessors)
            'is_on_sale' => $this->is_on_sale,
            'discount_percentage' => $this->discount_percentage,

            // Status
            'status' => $this->status?->value,
            'status_label' => $this->status?->label(),

            // Boolean Fields
            'is_featured' => $this->is_featured,

            // Relationships (whenLoaded)
            'category' => CategoryResource::make($this->whenLoaded('category')),
            'tags' => TagResource::collection($this->whenLoaded('tags')),
            'images' => {{Entity}}ImageResource::collection($this->whenLoaded('images')),
            'user' => UserResource::make($this->whenLoaded('user')),

            // Conditional Fields (based on user permission)
            'metadata' => $this->when(
                $request->user()?->can('viewMetadata', $this->resource),
                $this->metadata
            ),

            // Admin-only fields
            'internal_notes' => $this->when(
                $request->user()?->isAdmin(),
                $this->internal_notes
            ),

            // Counts (when loaded)
            'orders_count' => $this->when(
                $this->orders_count !== null,
                $this->orders_count
            ),
            'reviews_count' => $this->when(
                $this->reviews_count !== null,
                $this->reviews_count
            ),

            // Aggregates (when loaded)
            'average_rating' => $this->when(
                $this->average_rating !== null,
                fn () => round($this->average_rating, 1)
            ),

            // Pivot data (for many-to-many)
            'pivot' => $this->whenPivotLoaded('{{entity}}_tag', function () {
                return [
                    'sort_order' => $this->pivot->sort_order,
                ];
            }),

            // Timestamps
            'published_at' => $this->published_at?->toISOString(),
            'created_at' => $this->created_at->toISOString(),
            'updated_at' => $this->updated_at->toISOString(),
        ];
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param Request $request
     * @return array<string, mixed>
     */
    public function with(Request $request): array
    {
        return [
            'meta' => [
                'api_version' => 'v1',
            ],
        ];
    }

    /**
     * Customize the outgoing response for the resource.
     *
     * @param Request $request
     * @param \Illuminate\Http\JsonResponse $response
     * @return void
     */
    public function withResponse(Request $request, $response): void
    {
        $response->header('X-Resource-Type', '{{Entity}}');
    }
}

Step 3: Generate Collection Resource

<?php
// app/Http/Resources/{{Entity}}Collection.php
namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class {{Entity}}Collection extends ResourceCollection
{
    /**
     * The resource that this resource collects.
     *
     * @var string
     */
    public $collects = {{Entity}}Resource::class;

    /**
     * Transform the resource collection into an array.
     *
     * @param Request $request
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'data' => $this->collection,
        ];
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param Request $request
     * @return array<string, mixed>
     */
    public function with(Request $request): array
    {
        return [
            'meta' => [
                'total' => $this->total(),
                'per_page' => $this->perPage(),
                'current_page' => $this->currentPage(),
                'last_page' => $this->lastPage(),
                'from' => $this->firstItem(),
                'to' => $this->lastItem(),
Read more
@fujigo-software
Ships withf5-framework

AI-Powered Development Framework for Claude Code

2434 skills104 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
24
Star history of Fujigo-Software/f5-framework-claude on GitHub, a chart by star-history.com

Other agents on f5-framework.

More agents from other plugins.

a11y-architect
Auto-invoked

a11y-architect

Agent

Accessibility Architect specializing in WCAG 2.2 compliance for Web and Native platforms. Use PROACTIVELY when designing UI…

ecc11d293k
@affaan-m@affaan-mInstall Agent
analyze-pass-impact

analyze-pass-impact

Agent

Analyzes how a specific topic affects a group of compiler passes. Used by the /plan-update skill to parallelize research across…

react1d252k
@facebook@facebookInstall Agent
orchestrator

orchestrator

Agent

- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as…

codextoday135k
@openai@openaiInstall Agent
cavecrew-builder

cavecrew-builder

Agent

Surgical 1-2 file edit. Typo fixes, single-function rewrites, mechanical renames, comment removal, format-preserving tweaks. Hard…

caveman2d119k
@juliusbrussee@juliusbrusseeInstall Agent

Questions about resource-generator.

Which coding agents can use resource-generator?
resource-generator installs in Claude Code and Cursor: each loads subagents from .claude/agents, so copy the file there. Flowy's auto-invocation runs in Claude Code only.