onamfc/eloquent-json-schema 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

onamfc/eloquent-json-schema

Composer 安装命令:

composer require onamfc/eloquent-json-schema

包简介

Generate JSON Schema files from Eloquent models for Laravel applications

README 文档

README

Generate JSON Schema files from your Eloquent models for Laravel applications.

Features

  • Single Source of Truth: Infer schema from model casts, database columns, PHPDoc types, and PHP Attributes
  • Request & Response Schemas: Generate different schemas for API requests and responses
  • Composable Resolvers: Modular system for extracting schema information
  • PHP Attributes: Clean metadata injection with custom attributes
  • Validation Integration: Middleware for automatic request validation
  • OpenAPI Support: Export schemas for Swagger/Redoc documentation
  • Versioning: Track schema changes with version management
  • Artisan Commands: Easy generation, diffing, and publishing

Installation

composer require onamfc/eloquent-json-schema

Publish the configuration file:

php artisan vendor:publish --provider="onamfc\EloquentJsonSchema\LaravelSchemaServiceProvider"

Quick Start

1. Add Attributes to Your Models

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;
}

2. Generate Schemas

# Generate all model schemas
php artisan schema:generate

# Generate specific model
php artisan schema:generate User

3. Use in Validation

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

Generated Output

Schemas are saved to storage/api-schemas/{version}/models/:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.example.com/v1/models/User.response.schema.json",
  "title": "User",
  "type": "object",
  "properties": {
    "id": { "type": "string", "format": "uuid", "readOnly": true },
    "email": { "type": "string", "format": "email" },
    "plan": { "type": "string", "enum": ["basic","pro","enterprise"] }
  },
  "required": ["id","email","plan"]
}

Available Attributes

  • #[SchemaName('CustomName')] - Override model name in schema
  • #[Title('Field Title')] - Add title to property
  • #[Description('Field description')] - Add description to property
  • #[Format('email|uuid|date-time')] - Set format constraint
  • #[Enum(['value1', 'value2'])] - Define enum values
  • #[RequestOnly(['field1'])] - Fields only in request schema
  • #[ResponseOnly(['field1'])] - Fields only in response schema

Commands

# 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

Configuration

Key configuration options in config/laravel-schema.php:

  • version: Schema version identifier
  • output_directory: Where to save generated schemas
  • type_mapping: How to map database types to JSON Schema
  • relationship_depth: Whether to embed relationships or use ID references
  • decimal_as: Handle decimals as 'number' or 'string'

Validation Middleware

Register the middleware in your HTTP kernel:

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

Use in routes:

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

OpenAPI Integration

Export schemas as OpenAPI components:

php artisan schema:publish-openapi

Then reference in your OpenAPI spec:

paths:
  /users:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UserRequest'
      responses:
        201:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserResponse'

License

MIT License

onamfc/eloquent-json-schema 适用场景与选型建议

onamfc/eloquent-json-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 onamfc/eloquent-json-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 onamfc/eloquent-json-schema 我们能提供哪些服务?
定制开发 / 二次开发

基于 onamfc/eloquent-json-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-26