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
}
// 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
{
// ...
}