PHP code example of romegasoftware / laravel-schema-generator

1. Go to this page and download the library: Download romegasoftware/laravel-schema-generator 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/ */

    

romegasoftware / laravel-schema-generator example snippets


// config/laravel-schema-generator.php

'zod' => [
    'output' => [
        'path' => resource_path('js/types/schemas.ts'), // fallback/default path
        'separate_files' => true,
        // Optional: specify a dedicated directory for separate files
        'directory' => resource_path('js/types/schemas'),
    ],
],

use RomegaSoftware\LaravelSchemaGenerator\Attributes\ValidationSchema;

#[ValidationSchema]
class CreateUserRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => '

use RomegaSoftware\LaravelSchemaGenerator\Support\SchemaRule;

'items' => [
    '$fail, string $message): void {
            if (collect($value)->sum('qty') < 12) {
                $fail($message);
            }
        }
    )
        ->append(static function (string $encodedMessage): string {
            return <<<ZOD
                .superRefine((items, ctx) => {
                    const total = items.reduce((sum, item) => sum + item.qty, 0);
                    if (total < 12) {
                        ctx.addIssue({
                            code: 'custom',
                            message: {$encodedMessage},
                            path: ['items'],
                        });
                    }
                })
                ZOD;
        })
        ->failWith('You must order at least 12 total units.'),
],

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use RomegaSoftware\LaravelSchemaGenerator\Concerns\InteractsWithSchemaFragment;
use RomegaSoftware\LaravelSchemaGenerator\Contracts\SchemaAnnotatedRule;

final class TotalOrderItemsRule implements SchemaAnnotatedRule, ValidationRule
{
    use InteractsWithSchemaFragment;

    public function __construct()
    {
        $this->withFailureMessage('You must order at least 12 total cases.')
            ->append(function (?string $encodedMessage) {
                return <<<ZOD
                .superRefine((items, ctx) => {
                    const total = items.reduce((sum, item) => sum + item.quantity, 0);
                    if (total < 12) {
                        ctx.addIssue({
                            code: 'custom',
                            message: {$encodedMessage},
                            path: ['items']
                        });
                    }
                })
                ZOD;
            });
    }

    /**
     * Run the validation rule.
     *
     * @param  Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (collect($value)->sum('quantity') < 12) {
            $fail($this->failureMessage() ?? 'You must order at least 12 total units.');
        }
    }
}
bash
php artisan vendor:publish --provider="RomegaSoftware\LaravelSchemaGenerator\LaravelSchemaGeneratorServiceProvider"
bash
php artisan schema:generate