PHPackages                             andyabih/json-to-laravel-migrations - 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. andyabih/json-to-laravel-migrations

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

andyabih/json-to-laravel-migrations
===================================

Turn a JSON schema into Laravel migrations.

1.0.0(5y ago)111.3k7[2 PRs](https://github.com/andyabih/json-to-laravel-migrations/pulls)MITPHP

Since Mar 21Pushed 5y ago3 watchersCompare

[ Source](https://github.com/andyabih/json-to-laravel-migrations)[ Packagist](https://packagist.org/packages/andyabih/json-to-laravel-migrations)[ RSS](/packages/andyabih-json-to-laravel-migrations/feed)WikiDiscussions main Synced 2d ago

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

JSON to Laravel Migrations
==========================

[](#json-to-laravel-migrations)

Simply create a .json file with the schema for your database, and run the artisan command `json:migrate schema.json` to create all the migrations for your project.

**Note:** This package is built to be used to kickstart your Laravel projects, and not to use on something that's already been built.

---

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

[](#installation)

You can install this package by running the below composer command:

```
composer require --dev andyabih/json-to-laravel-migrations

```

---

Creating the JSON schema
------------------------

[](#creating-the-json-schema)

Create a `schema.json` file in the root of your project, and use a template like the below:

```
{
    "posts": {
        "name"   : "string:50|index:50",
        "state"  : "enum:active,inactive|default:active",
        "text"   : "text",
        "slug"   : "string:50|unique",
        "active" : "boolean|default:false",
        "user_id": "foreign|nullable|constrained|onDelete"
    },

    "categories": {
        "name" : "string",
        "image": "string"
    },

    "subcategories": {
        "name"       : "string",
        "category_id": "foreign|constrained"
    }
}
```

The main keys of your JSON represent the table names. Make sure to create them in order in case a table has a relationship with another. In this case, `posts`, `categories`, and `subcategories` are our tables.

Next, for each table, define your columns as keys (so `name`, `state`, `text`, ... in this case), and set their properties.

Properties
----------

[](#properties)

Properties are separated with a pipe (`|`), and the first property should always be the column type. The package supports every column type in Laravel.

Additional options (such as string length) can be supplied using a colon (`:`), followed by the value of the option. Multiple options can be supplied (for `float`, for example).

Migrations
----------

[](#migrations)

Run the above using:

```
php artisan json:migrate schema.json

```

The above schema will create something three different migrations. The `posts` schema will look like:

```
