PHP code example of onamfc / eloquent-json-schema

1. Go to this page and download the library: Download onamfc/eloquent-json-schema 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/ */

    

onamfc / eloquent-json-schema example snippets


use App\Schemas as S;

#[S\SchemaName('User')]
#[S\RequestOnly(['password'])]
class User extends Model {
    #[S\Title('User ID')]
    #[S\Format('uuid')]
    public $id;

    #[S\Description('Primary email')]
    #[S\Format('email')]
    public $email;

    #[S\Enum(['basic','pro','enterprise'])]
    public $plan;
}

Route::post('/users', [UserController::class, 'store'])
    ->middleware('schema:User.request');

protected $middlewareAliases = [
    'schema' => \onamfc\EloquentJsonSchema\Http\Middleware\ValidateWithSchemaMiddleware::class,
];

Route::post('/api/users', [UserController::class, 'store'])
    ->middleware('schema:User.request');
bash
php artisan vendor:publish --provider="onamfc\EloquentJsonSchema\LaravelSchemaServiceProvider"
bash
# Generate schemas
php artisan schema:generate

# Show differences between versions
php artisan schema:diff --from=v1 --to=v2

# Export OpenAPI components
php artisan schema:publish-openapi
bash
php artisan schema:publish-openapi