PHP code example of wappo / laravel-schema-api

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

    

wappo / laravel-schema-api example snippets


use Wappo\LaravelSchemaApi\Attributes\ApiInclude;

class Order extends Model
{
    #[ApiInclude]
    public function rows(): HasMany
    {
        return $this->hasMany(OrderRow::class);
    }

    #[ApiInclude]
    public function owner(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

use Wappo\LaravelSchemaApi\Attributes\ApiInclude;
use Wappo\LaravelSchemaApi\Concerns\HasApiIncludeCascadeDelete;

class Order extends Model
{
    use HasApiIncludeCascadeDelete, SoftDeletes;

    // Related rows will be automatically deleted when order is deleted
    #[ApiInclude(cascadeDelete: true)]
    public function rows(): HasMany
    {
        return $this->hasMany(OrderRow::class);
    }

    // For models without SoftDeletes, you must explicitly allow hard deletion
    #[ApiInclude(cascadeDelete: true, forceDelete: true)]
    public function metadata(): HasOne
    {
        return $this->hasOne(OrderMetadata::class);
    }
}

// varchar(255) → string|max:255
// integer → integer
// datetime → date
// etc.

use Wappo\LaravelSchemaApi\Attributes\UseValidationRulesProvider;
use Wappo\LaravelSchemaApi\Contracts\ValidationRulesProviderInterface;

#[UseValidationRulesProvider(PostValidationRules::class)]
class Post extends Model
{
    // ...
}

class PostValidationRules implements ValidationRulesProviderInterface
{
    public function getRules(): array
    {
        return [
            'title' => '

use Wappo\LaravelSchemaApi\Attributes\ApplyQueryModifier;
use Wappo\LaravelSchemaApi\QueryModifiers\LatestFirstModifier;
use Wappo\LaravelSchemaApi\QueryModifiers\FilterQueryModifier;

#[ApplyQueryModifier(LatestFirstModifier::class)]
#[ApplyQueryModifier(FilterQueryModifier::class)]
class Post extends Model
{
    // Posts will be ordered by created_at DESC
    // and support filtering via query parameters
}

use Wappo\LaravelSchemaApi\Attributes\ApiIgnore;

#[ApiIgnore]
class AdminUser extends Model
{
    // This model will NOT be exposed via the API
}

#[ApiIgnore(shouldBroadcast: true)]
class InternalAuditLog extends Model
{
    // Not exposed via HTTP, but changes are broadcast to authorized users
}

// config/schema-api.php
'broadcasting' => [
    'enabled' => true,
    'mode' => 'model-events', // or 'sync'
],

// In your AuthServiceProvider
Gate::define('view', function (User $user, Model $model) {
    // Return true if $user can view $model
    return $user->id === $model->user_id;
});

use Wappo\LaravelSchemaApi\Contracts\ModelViewAuthorizerInterface;

$this->app->singleton(ModelViewAuthorizerInterface::class, function () {
    return new YourCustomAuthorizer();
});

use Wappo\LaravelSchemaApi\Attributes\UseSchemaApiJsonResource;

#[UseSchemaApiJsonResource(PostResource::class)]
class Post extends Model
{
    // ...
}

return [
    'date_format' => 'Y-m-d\TH:i:s.u\Z', // ISO 8601 format

    'http' => [
        'base_path' => '/schema-api',
        'middleware' => ['api'],
        'gzip_level' => 6, // 0-9, compression level
        'relationship_batch_size' => 200, // Batch size for loading relationships
    ],

    'broadcasting' => [
        'enabled' => false, // Enable WebSocket broadcasting
        'mode' => 'sync', // 'sync' or 'model-events'
    ],

    'restore_soft_delete_tolerance_in_seconds' => 1, // Tolerance for cascade restore

    'model_resolver' => [
        'driver' => 'namespace', // 'namespace' or 'morph-map'
    ],

    'resource_resolver' => [
        'driver' => 'namespace', // How to find JSON resource classes
    ],
];
bash
php artisan vendor:publish --tag="schema-api-config"
bash
# Get posts updated since 2025-01-01
GET /schema-api/posts?since=2025-01-01T00:00:00Z
bash
php artisan app:generate-client-resources