PHP code example of predatorstudio / laravel-attribute-code-generators

1. Go to this page and download the library: Download predatorstudio/laravel-attribute-code-generators library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

predatorstudio / laravel-attribute-code-generators example snippets


'paths' => [
    'controllers'  => 'app/Http/Controllers',
    'resources'    => 'app/Http/Resources',
    'requests'     => 'app/Http/Requests',
    'services'     => 'app/Services',
    'repositories' => 'app/Repositories',
    'contracts'    => 'app/Contracts',
    'policies'     => 'app/Policies',
    'observers'    => 'app/Observers',
    'actions'      => 'app/Actions',
    'dto'          => 'app/DTO',
    'enums'        => 'app/Enums',
],

'paths' => [
    'services' => 'app/Libs/Services',
],

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

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

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

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

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

#[DTO]

#[Policy]

#[Observer]

#[Factory]

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

#[UseSoftDeletes]

#[GenerateMigration]

#[ValidateFromMigration]

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

#[BackedEnum(field: 'status', values: ['active', 'inactive'], filename: 'ProjectStatus')]
// generates App\Enums\ProjectStatus instead of App\Enums\ProjectStatus (default)

#[Action]

#[GenerateTest]

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'],
    ];
}

['name' => 'author_id', 'type' => 'foreignId', 'relation' => [
    'model'   => 'User',      // related model class (default: auto-derived from field name)
    'local'   => 'author_id', // local key on this table (default: field name)
    'foreign' => 'id',        // key on the related table (default: id)
]]

public function author(): BelongsTo
{
    return $this->belongsTo(User::class, 'author_id', 'id');
}

public function register(): void
{
    // @crud-generator:start
    $this->app->bind(\App\Contracts\UserServiceInterface::class, \App\Services\UserService::class);
    // @crud-generator:end

    // your manual bindings go here and are never touched
    $this->app->bind(PaymentGatewayInterface::class, StripeGateway::class);
}

'generate_php_docs' => true,

/** @return AnonymousResourceCollection */
public function index(): AnonymousResourceCollection { ... }

/** @return UserResource */
public function show(User $user): UserResource { ... }

/** @return Response */
public function destroy(User $user): Response { ... }

/** @return LengthAwarePaginator */
public function index(): \Illuminate\Contracts\Pagination\LengthAwarePaginator { ... }

/** @return User */
public function store(array $data): User { ... }



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Action;
use Vendor\LaravelAttributeCodeGenerators\Attributes\BackedEnum;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Crud;
use Vendor\LaravelAttributeCodeGenerators\Attributes\DTO;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Factory;
use Vendor\LaravelAttributeCodeGenerators\Attributes\GenerateTest;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Observer;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Policy;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Repository;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Resource;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Route;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Seeder;
use Vendor\LaravelAttributeCodeGenerators\Attributes\Service;
use Vendor\LaravelAttributeCodeGenerators\Attributes\UseSoftDeletes;
use Vendor\LaravelAttributeCodeGenerators\Attributes\ValidateFromMigration;

#[Crud(methods: ['index', 'store', 'show', 'update', 'destroy'])]
#[Route(path: 'users', middleware: ['auth:sanctum'])]
#[Resource(fields: ['id', 'name', 'email'])]
#[Service(interface: true)]
#[Repository(interface: true)]
#[Policy]
#[ValidateFromMigration]
#[DTO]
#[UseSoftDeletes]
#[BackedEnum(field: 'status', values: ['active', 'inactive'])]
#[Observer]
#[Action]
#[Factory]
#[Seeder(count: 20)]
#[GenerateTest]
class User extends Model
{
    public function fields(): array
    {
        return [
            ['type' => 'id'],
            ['name' => 'name',        'type' => 'string'],
            ['name' => 'email',       'type' => 'string', 'unique' => true],
            ['name' => 'bio',         'type' => 'text',   'nullable' => true],
            ['name' => 'password',    'type' => 'string', 'hidden' => true],
            ['name' => 'team_id',     'type' => 'foreignId', 'relation' => [
                'model' => 'Team',
            ]],
            ['type' => 'timestamps'],
        ];
    }
}
bash
php artisan vendor:publish --tag=crud-generator-config
bash
php artisan crud:install --ai

app/Models/Projects/Project.php  →  App\Models\Projects\Project

app/Http/Controllers/Projects/ProjectController.php
app/Services/Projects/ProjectService.php
app/Repositories/Projects/ProjectRepository.php
...
bash
php artisan crud:sync