PHPackages                             mojopollo/laravel-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. [Database &amp; ORM](/categories/database)
4. /
5. mojopollo/laravel-json-schema

ActiveLibrary[Database &amp; ORM](/categories/database)

mojopollo/laravel-json-schema
=============================

Create all your migrations and models from one JSON schema file. Laravel Database Schema in JSON allows you to define your entire Laravel database schema in one JSON file then generates all the necessary migration files

5.4.1(8y ago)11354510[1 issues](https://github.com/mojopollo/laravel-json-schema/issues)[1 PRs](https://github.com/mojopollo/laravel-json-schema/pulls)MITPHPPHP &gt;=5.4.0

Since Mar 10Pushed 7y ago8 watchersCompare

[ Source](https://github.com/mojopollo/laravel-json-schema)[ Packagist](https://packagist.org/packages/mojopollo/laravel-json-schema)[ Docs](http://localhost)[ RSS](/packages/mojopollo-laravel-json-schema/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (29)Used By (0)

JSON database schema for Laravel
================================

[](#json-database-schema-for-laravel)

[![Build Status](https://camo.githubusercontent.com/addbd88214ecba505b7cf41ef5f3e3f0c8ae63b0af0702656d9dfd5917f6f756/68747470733a2f2f7472617669732d63692e6f72672f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mojopollo/laravel-json-schema)[![Coverage Status](https://camo.githubusercontent.com/fb0b8802b8c206240f976ee35aef3a35be75632c4345127c67206aeed9effe51/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mojopollo/laravel-json-schema?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/7d31d823dcff01601095387bb0e644123aec5c52c91b391b9b8bf1daedc74781/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612f762f737461626c65)](https://packagist.org/packages/mojopollo/laravel-json-schema)[![Latest Unstable Version](https://camo.githubusercontent.com/45420af21f212977f3646746c5744c71e85ce57523da4dffd42eae01d282c2e8/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612f762f756e737461626c65)](https://packagist.org/packages/mojopollo/laravel-json-schema)[![License](https://camo.githubusercontent.com/be10aa783326949f4c76eb95e0a6928a717bd65dbce18882bf319eba1250af1b/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612f6c6963656e7365)](https://packagist.org/packages/mojopollo/laravel-json-schema)[![Total Downloads](https://camo.githubusercontent.com/498de57257495616c5b9a7db69603a1e0f238be06dd2eb4178baa4b9f4852350/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6a6f706f6c6c6f2f6c61726176656c2d6a736f6e2d736368656d612f646f776e6c6f616473)](https://packagist.org/packages/mojopollo/laravel-json-schema)

Create all your migrations and models from one JSON schema file. This package allows you to define your entire [Laravel](https://github.com/laravel/laravel) database schema in one JSON file then generates all the necessary migration files. Makes use of Jeffrey Way's [Extended Generators](https://github.com/laracasts/Laravel-5-Generators-Extended).

[![preview-01](https://cloud.githubusercontent.com/assets/1254915/14058470/9834f110-f2de-11e5-9c50-4ceffa0c9f88.jpg)](https://cloud.githubusercontent.com/assets/1254915/14058470/9834f110-f2de-11e5-9c50-4ceffa0c9f88.jpg)

- [Versions](#versions)
- [Installation](#installation)
- [Usage](#usage)
    - [Create your schema in JSON](#usage-create)
    - [Generate your migrations](#usage-generate)
    - [Pivot tables](#usage-pivot)
    - [Undo](#usage-undo)
    - [Validation](#usage-validation)
- [JSON File Examples](#json-file-examples)

Versions
--------

[](#versions)

For Laravel 5.4.x and above use laravel-json-schema tag 5.4.x. You might need to set your composer.json minimum-stability to `dev` :

```
"minimum-stability": "dev"

```

For Laravel 5.3.x and below use laravel-json-schema tag 1.x.x

Installation
------------

[](#installation)

#### Step 1: Add package via composer

[](#step-1-add-package-via-composer)

Add this package to your `composer.json` file with the following command

```
composer require mojopollo/laravel-json-schema --dev
```

#### Step 2: Add the service providers

[](#step-2-add-the-service-providers)

Add the following 2 service providers to your local environment only, by modifying your `app/Providers/AppServiceProvider.php` as so:

```
public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Mojopollo\Schema\MakeMigrationJsonServiceProvider');
        $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
    }
}
```

Usage
-----

[](#usage)

#### Create your schema in JSON

[](#create-your-schema-in-json)

Create your JSON schema file and save as `schema.json` for example:

```
{
  "users": {
    "email": "string:unique",
    "password": "string:index",
    "first_name": "string:nullable",
    "last_name": "string:nullable",
    "last_active_at": "timestamp:nullable:index"
  },
  "categories": {
    "name": "string:unique"
  }
}
```

#### Generate your migrations

[](#generate-your-migrations)

If you have your JSON file, you can now generate all your migrations, using the `--file=` option to specify where the JSON file is located:

```
php artisan make:migration:json --file=schema.json
```

After this command executes you will see all the newly created migration files, example output:

```
Model created successfully.
Migration created successfully.
Model created successfully.
Migration created successfully.
The following files have been created:
  app/CartItem.php
  app/Category.php
  database/migrations/2016_03_13_231727_create_categories_table.php
  database/migrations/2016_03_13_231728_create_tags_table.php
```

If you have a extensive long schema json file and want to only generate specific tables or migrations from the schema, you would do the following:

```
php artisan make:migration:json --file=schema.json --only=categories,tags
```

In the above example, the tables or migrations named "categories" and "tags" will be generated and all other tables/migrations will be ignored.

#### Pivot tables

[](#pivot-tables)

If you need to generate a pivot table, you will append `_pivot` to your migration name, for example:

```
{
  "posts_tags_pivot": null
}
```

This will create a pivot table migration for the tables `posts` and `tags`

#### Undo

[](#undo)

To undo and delete all files that where previously generated with the json file that was used, example:

```
php artisan make:migration:json --file=schema.json --undo
```

What this will do is look for the `schema.json.undo.json` file if it exists, read the contents and remove all files that where generated, example output:

```
Deleting files:
  Deleted: app/CartItem.php
  Deleted: app/Category.php
  Deleted: database/migrations/2016_03_13_231727_create_categories_table.php
  Deleted: database/migrations/2016_03_13_231728_create_tags_table.php
```

If you prefer not to create a "undo file" in the same directory as the source json file, use the `--disableundo` option at the time of migration generation:

```
php artisan make:migration:json --file=schema.json --disableundo
```

This will prevent the creation of a undo file.

#### Validation

[](#validation)

To check your json file for valid json syntax and schema validation (column type definitions and column type modifiers checks):

```
php artisan make:migration:json --file=schema.json --validate
```

Note: this does not generate any migration files and will just check if you misspelled any field schema definitions

JSON File Examples
------------------

[](#json-file-examples)

#### Using table names or migration names

[](#using-table-names-or-migration-names)

You can use table names or use a migration name that [Extended Generators](https://github.com/laracasts/Laravel-5-Generators-Extended) will understand.

For example:

```
{
  "users": {
    "email": "string:unique",
    "password": "string:index"
  }
}
```

Is the same as:

```
{
  "create_users_table": {
    "email": "string:unique",
    "password": "string:index"
  }
}
```

#### Putting it all together

[](#putting-it-all-together)

You can now get crazy with defining your entire database schema and having the benefit of seeing it all in one file. As you have seen we can `--undo` to remove all previously generated files from the last command then make edits to our JSON file, validate the syntax with `--validate` and then generate it all over again. One word: **WOW**. :)

```
{
  "users": {
    "email": "string:unique",
    "password": "string:index"
  },

  "create_cats_table": {
    "name": "string:unique"
  },

  "remove_user_id_from_posts_table": {
    "name": "user_id:integer"
  },

  "posts_tags_pivot": null
}
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 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

Every ~19 days

Recently: every ~119 days

Total

28

Last Release

3202d ago

Major Versions

1.0.25 → 5.4.02017-08-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e734bd29e3205ae6d927eb522b0b6ef096be8fec7f57f4ccee2983321977e2a?d=identicon)[mojopollo](/maintainers/mojopollo)

---

Top Contributors

[![mojopollo](https://avatars.githubusercontent.com/u/1254915?v=4)](https://github.com/mojopollo "mojopollo (97 commits)")

---

Tags

database-schemaextended-generatorsjson-schemalaravelpivot-tablesjsonlaravelschemadatabasemojomojopollolaravel migrations from json

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mojopollo-laravel-json-schema/health.svg)

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

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)[dragon-code/laravel-data-dumper

Adding data from certain tables when executing the `php artisan schema:dump` console command

3418.6k](/packages/dragon-code-laravel-data-dumper)

PHPackages © 2026

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