PHPackages                             predatorstudio/laravel-attribute-code-generators - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. predatorstudio/laravel-attribute-code-generators

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

predatorstudio/laravel-attribute-code-generators
================================================

v1.0(1mo ago)03MITPHPPHP ^8.1

Since Apr 23Pushed 1w agoCompare

[ Source](https://github.com/PredatorStudio/LaravelAttributeCodeGenerators)[ Packagist](https://packagist.org/packages/predatorstudio/laravel-attribute-code-generators)[ RSS](/packages/predatorstudio-laravel-attribute-code-generators/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Laravel Attribute Code Generators
=================================

[](#laravel-attribute-code-generators)

**v1.0**

A Laravel package that generates a full CRUD scaffold from PHP 8.2 Attributes placed directly on Eloquent models. One command reads every model in your app, inspects its attributes, and writes controllers, services, repositories, DTOs, resources, migrations, policies, observers, factories, seeders, tests, and routes — only what you asked for, nothing more.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11, 12, or 13

Installation
------------

[](#installation)

```
composer require predatorstudio/laravel-attribute-code-generators
```

The service provider is auto-discovered via Laravel's package auto-discovery.

Publish the config file to customise paths:

```
php artisan vendor:publish --tag=crud-generator-config
```

This creates `config/crud-generator.php` in your application.

Claude Code integration (optional)
----------------------------------

[](#claude-code-integration-optional)

Install a `/describe-attributes` slash command into your project's Claude Code environment:

```
php artisan crud:install --ai
```

This copies a prompt file to `.claude/commands/describe-attributes.md` in your project root. After restarting Claude Code the command is available as a slash command:

CommandDescription`/describe-attributes`Full reference — every attribute, its parameters, generated artifacts, and interaction map`/describe-attributes Seeder`Reference for a single attribute (case-insensitive)The skill covers:

- What each attribute generates
- All constructor parameters with types, defaults, and descriptions
- Usage examples
- Non-obvious behaviours (idempotency, PATCH semantics, `hidden` flag, ALTER migrations, …)
- `fields()` key reference
- Attribute interaction map (which attributes depend on or complement each other)

Configuration
-------------

[](#configuration)

After publishing the config you can adjust these keys in `config/crud-generator.php`:

KeyDefaultDescription`scan_path``app/Models`Directory scanned for models (relative to project root). All subdirectories are included recursively. The namespace is derived automatically from the path.`api_docs_path``docs/api`Base directory for API documentation files`api_docs_models_path``docs/api/models`Directory where per-model YAML files are saved`api_docs_main_file``docs/api/openapi.yaml`Main OpenAPI file regenerated on every `crud:sync`### Scanning subdirectories

[](#scanning-subdirectories)

Models can be placed in any subdirectory under `scan_path`. The namespace is inferred from the folder structure:

```
app/Models/Blog/Post.php     → App\Models\Blog\Post
app/Models/Shop/Product.php  → App\Models\Shop\Product

```

To scan a different root, change `scan_path` in the config:

```
'scan_path' => 'app/Domain/Models',
// resolves to namespace App\Domain\Models
```

Usage
-----

[](#usage)

Annotate your model with the attributes that describe what you want generated, then run:

```
php artisan crud:sync
```

OptionDescription`--dry-run`Preview what would be generated without writing any files`--force`Overwrite existing files (asks for confirmation per file)Available Attributes
--------------------

[](#available-attributes)

### `#[Crud]`

[](#crud)

Enables CRUD generation for the model. Optionally restricts which HTTP methods are scaffolded. The generated controller and resource routes will contain only the listed methods.

```
#[Crud]                                                    // all methods
#[Crud(methods: ['index', 'store', 'show', 'update', 'destroy'])]
```

### `#[Route]`

[](#route)

Registers the resource route under the given path. Optionally applies middleware to the route group.

```
#[Route(path: 'users')]
#[Route(path: 'users', middleware: ['auth:sanctum', 'verified'])]
```

### `#[Resource]`

[](#resource)

Generates an API resource class. Specify which fields to expose, or omit `fields` to auto-generate from the model's visible `fields()` columns (respecting `hidden: true`).

```
#[Resource]
#[Resource(fields: ['id', 'name', 'email'])]
```

### `#[Service]`

[](#service)

Generates a service class. Pass `interface: true` to also generate a contract and bind it in the service container.

```
#[Service]
#[Service(interface: true)]
```

### `#[Repository]`

[](#repository)

Generates a repository class. Pass `interface: true` to also generate a contract and bind it.

```
#[Repository]
#[Repository(interface: true)]
```

### `#[DTO]`

[](#dto)

Generates a Data Transfer Object with readonly properties, a `fromArray()` factory, and a `toArray()` method. Fields marked `hidden: true` in `fields()` are excluded.

```
#[DTO]
```

### `#[Policy]`

[](#policy)

Generates a policy class for the model.

```
#[Policy]
```

### `#[Observer]`

[](#observer)

Generates an observer class and registers it automatically.

```
#[Observer]
```

### `#[Factory]`

[](#factory)

Generates an Eloquent factory for the model with sensible faker defaults per column type.

```
#[Factory]
```

### `#[Seeder]`

[](#seeder)

Generates a database seeder that uses the model's factory to create records. The default count is 10.

```
#[Seeder]
#[Seeder(count: 50)]
```

### `#[SoftDeletes]`

[](#softdeletes)

Adds soft-delete support: appends `$table->softDeletes()` to the generated migration and injects the `SoftDeletes` trait into the model.

```
#[SoftDeletes]
```

### `#[GenerateMigration]`

[](#generatemigration)

Generates a `create_*_table` migration based on the model's `fields()` method. On subsequent runs the package compares saved column names against the current `fields()` list — if new columns are found it generates an `add_columns_to_*_table` ALTER migration instead of recreating the original.

```
#[GenerateMigration]
```

### `#[ValidateFromMigration]`

[](#validatefrommigration)

Generates `StoreRequest` and `UpdateRequest` validation rules derived from the migration columns. The `UpdateRequest` automatically prepends `sometimes` to every rule, making it suitable for PATCH requests (only fields present in the payload are validated).

Fields marked `hidden: true` in `fields()` are excluded from both requests.

```
#[ValidateFromMigration]
```

### `#[BackedEnum]`

[](#backedenum)

Generates a backed enum for a field. Repeatable — add one per enum field.

```
#[BackedEnum(field: 'status', values: ['active', 'inactive'])]
#[BackedEnum(field: 'role',   values: ['admin', 'editor', 'viewer'], type: 'string')]
```

### `#[Action]`

[](#action)

Generates single-purpose action classes (`Create`, `Update`, `Delete`) for the model.

```
#[Action]
```

### `#[GenerateTest]`

[](#generatetest)

Generates a feature test for the model's CRUD endpoints.

```
#[GenerateTest]
```

The `fields()` method
---------------------

[](#the-fields-method)

Define the model's schema by implementing a `fields()` method. It drives migration generation, validation rules, factories, DTOs, and resource output.

```
public function fields(): array
{
    return [
        ['type' => 'id'],
        ['name' => 'title',       'type' => 'string'],
        ['name' => 'body',        'type' => 'text',      'nullable' => true],
        ['name' => 'status',      'type' => 'string',    'default' => 'draft'],
        ['name' => 'user_id',     'type' => 'foreignId'],
        ['name' => 'secret_hash', 'type' => 'string',    'hidden' => true],
        ['type' => 'timestamps'],
    ];
}
```

KeyTypeDescription`name`stringColumn name`type`stringColumn type (`string`, `text`, `integer`, `boolean`, `foreignId`, `json`, `id`, `timestamps`, …)`nullable`boolAdds `nullable()` to the migration column and `nullable` to validation rules`unique`boolAdds `unique()` to the migration column`default`mixedAdds `->default(value)` to the migration column`hidden`boolExcludes the field from requests, resources, and DTOs — useful for internal columns like hashed tokens or audit fieldsFull example
------------

[](#full-example)

```
