mojopollo / laravel-json-schema by mojopollo

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
541
112
8
Package Data
Maintainer Username: mojopollo
Maintainer Contact: jmedina@qujo.com (Jaime Medina)
Package Create Date: 2016-03-04
Package Last Update: 2018-10-11
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-24 15:06:01
Package Statistics
Total Downloads: 541
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 112
Total Watchers: 8
Total Forks: 10
Total Open Issues: 3

JSON database schema for Laravel

Build Status Coverage Status Latest Stable Version Latest Unstable Version License Total Downloads

Create all your migrations and models from one JSON schema file. This package allows you to define your entire Laravel database schema in one JSON file then generates all the necessary migration files. Makes use of Jeffrey Way's Extended Generators.

preview-01

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

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

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

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

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

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

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

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

Using table names or migration names

You can use table names or use a migration name that Extended Generators 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

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
}