All Projects → mojopollo → Laravel Json Schema

mojopollo / Laravel Json Schema

Licence: mit
Create all your migrations and models from one JSON schema file.

Projects that are alternatives of or similar to Laravel Json Schema

Mysql Workbench Export Laravel 5 Migrations
A MySQL Workbench plugin which exports a Model to Laravel 5 Migrations
Stars: ✭ 876 (+767.33%)
Mutual labels:  database-schema, laravel
Quicksand
Easily schedule regular cleanup of old soft-deleted Eloquent data.
Stars: ✭ 259 (+156.44%)
Mutual labels:  laravel, pivot-tables
Generators
Laravel File Generators with config and publishable stubs
Stars: ✭ 102 (+0.99%)
Mutual labels:  laravel, pivot-tables
Laravel Json Schema Assertions
JSON Schema assertions for the Laravel framework
Stars: ✭ 61 (-39.6%)
Mutual labels:  json-schema, laravel
Laravel Table
Generate tables from Eloquent models.
Stars: ✭ 101 (+0%)
Mutual labels:  laravel
Laravel Backup Server
Backup multiple servers
Stars: ✭ 100 (-0.99%)
Mutual labels:  laravel
Ajv I18n
Internationalised error messages for Ajv JSON-Schema validator
Stars: ✭ 98 (-2.97%)
Mutual labels:  json-schema
Addchat Laravel
AddChat Laravel is a Laravel chat package. Live chat widget for Laravel that also includes multi-user chat, group permissions, customer support chat & more.
Stars: ✭ 99 (-1.98%)
Mutual labels:  laravel
Roles
Powerful package for handling roles in Laravel
Stars: ✭ 102 (+0.99%)
Mutual labels:  laravel
Laravel Reddit
Reddit clone built with Laravel 5
Stars: ✭ 101 (+0%)
Mutual labels:  laravel
Laravel Storage
A simple filesystem abstraction package for Laravel 4.
Stars: ✭ 100 (-0.99%)
Mutual labels:  laravel
Ecommerce Laravel Bootstrap
Responsive, Multi-Vendor, MultiLanguage Online Store Platform (shopping cart solution)
Stars: ✭ 99 (-1.98%)
Mutual labels:  laravel
Asset Cdn
Serve Laravel Assets from a Content Delivery Network (CDN)
Stars: ✭ 101 (+0%)
Mutual labels:  laravel
Laravel Permission
Associate users with roles and permissions
Stars: ✭ 10,024 (+9824.75%)
Mutual labels:  laravel
Laravel Settings
Persistent key-value storage for Laravel, json value supported. l10n supported.
Stars: ✭ 101 (+0%)
Mutual labels:  laravel
Laravel Seo Tools
Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard
Stars: ✭ 99 (-1.98%)
Mutual labels:  laravel
Dingo
Data access in Go - Code Generator
Stars: ✭ 100 (-0.99%)
Mutual labels:  database-schema
Socialnetwork
Laravel and Vue.JS powerd social network
Stars: ✭ 101 (+0%)
Mutual labels:  laravel
Elasticsearch Eloquent
⚡️ Eloquent models for Elasticsearch.
Stars: ✭ 100 (-0.99%)
Mutual labels:  laravel
Laravel Filemanager Example 5.3
Demo integration for laravel-filemanager (https://github.com/UniSharp/laravel-filemanager).
Stars: ✭ 100 (-0.99%)
Mutual labels:  laravel

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
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].