PHPackages                             onamfc/eloquent-json-schema - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. onamfc/eloquent-json-schema

ActiveLibrary

onamfc/eloquent-json-schema
===========================

Generate JSON Schema files from Eloquent models for Laravel applications

1.0.2(8mo ago)90MITPHPPHP ^8.1

Since Aug 26Pushed 8mo agoCompare

[ Source](https://github.com/onamfc/eloquent-to-json-generator)[ Packagist](https://packagist.org/packages/onamfc/eloquent-json-schema)[ RSS](/packages/onamfc-eloquent-json-schema/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (2)Used By (0)

Eloquent JSON Schema
====================

[](#eloquent-json-schema)

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

Features
--------

[](#features)

- **Single Source of Truth**: Infer schema from model casts, database columns, PHPDoc types, and PHP Attributes
- **Request &amp; 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
------------

[](#installation)

```
composer require onamfc/eloquent-json-schema
```

Publish the configuration file:

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

Quick Start
-----------

[](#quick-start)

### 1. Add Attributes to Your Models

[](#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

[](#2-generate-schemas)

```
# Generate all model schemas
php artisan schema:generate

# Generate specific model
php artisan schema:generate User
```

### 3. Use in Validation

[](#3-use-in-validation)

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

Generated Output
----------------

[](#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
--------------------

[](#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
--------

[](#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
-------------

[](#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
---------------------

[](#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
-------------------

[](#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
-------

[](#license)

MIT License

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance60

Regular maintenance activity

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

256d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/015bb990415173ada3a81ab055dc667409d64ad41bf8c94c433915210100e36e?d=identicon)[sitetransiiton](/maintainers/sitetransiiton)

---

Top Contributors

[![onamfc](https://avatars.githubusercontent.com/u/8187699?v=4)](https://github.com/onamfc "onamfc (5 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/onamfc-eloquent-json-schema/health.svg)

```
[![Health](https://phpackages.com/badges/onamfc-eloquent-json-schema/health.svg)](https://phpackages.com/packages/onamfc-eloquent-json-schema)
```

###  Alternatives

[hotmeteor/spectator

Testing helpers for your OpenAPI spec

3021.4M1](/packages/hotmeteor-spectator)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[spurwork/spectator

Testing helpers for your OpenAPI spec

3021.5k](/packages/spurwork-spectator)[carsdotcom/laravel-json-schema

Json Schema validation for Laravel projects

1036.7k3](/packages/carsdotcom-laravel-json-schema)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
