mojopollo / laravel-json-schema
从单个JSON模式文件创建所有迁移和模型。使用JSON格式的Laravel数据库模式,您可以在一个JSON文件中定义整个Laravel数据库模式,然后生成所有必要的迁移文件。
Requires
- php: >=5.4.0
- illuminate/database: ~5.0
- illuminate/filesystem: ~5.0
- illuminate/support: ~5.0
- laracasts/generators: dev-master#4e9ce5db9d93475ca27b993a92de3b15090aa139
Requires (Dev)
- phpunit/phpunit: 4.7.*
README
从单个JSON模式文件创建所有迁移和模型。此包允许您在单个JSON文件中定义整个Laravel数据库模式,然后生成所有必要的迁移文件。利用Jeffrey Way的扩展生成器。
版本
对于Laravel 5.4.x及以上版本,请使用laravel-json-schema标签5.4.x。您可能需要将composer.json的minimum-stability设置为dev
"minimum-stability": "dev"
对于Laravel 5.3.x及以下版本,请使用laravel-json-schema标签1.x.x
安装
步骤1:通过composer添加包
使用以下命令将此包添加到您的composer.json
文件中:
composer require mojopollo/laravel-json-schema --dev
步骤2:添加服务提供者
将以下2个服务提供者添加到您的本地环境,通过修改您的app/Providers/AppServiceProvider.php
如下:
public function register() { if ($this->app->environment() == 'local') { $this->app->register('Mojopollo\Schema\MakeMigrationJsonServiceProvider'); $this->app->register('Laracasts\Generators\GeneratorsServiceProvider'); } }
用法
在JSON中创建您的模式
创建您的JSON模式文件并将其保存为schema.json
等示例
{ "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" } }
生成您的迁移
如果您有JSON文件,现在可以使用--file=
选项指定JSON文件的位置来生成所有迁移
php artisan make:migration:json --file=schema.json
在此命令执行后,您将看到所有新创建的迁移文件,示例输出
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
如果您有一个庞大的长JSON模式文件,并且只想从模式中生成特定的表或迁移,您将执行以下操作:
php artisan make:migration:json --file=schema.json --only=categories,tags
在上面的示例中,将生成名为"categories"和"tags"的表或迁移,并忽略所有其他表/迁移。
交叉表
如果您需要生成交叉表,请将_pivot
追加到迁移名称中,例如
{ "posts_tags_pivot": null }
这将创建posts
和tags
表的交叉表迁移。
撤销
要撤销并删除之前使用该JSON文件生成的所有文件,示例
php artisan make:migration:json --file=schema.json --undo
这将查找schema.json.undo.json
文件(如果存在),读取其内容并删除所有生成的文件,示例输出
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
如果您希望在源JSON文件所在的同一目录中不创建"撤销文件",则在迁移生成时使用--disableundo
选项
php artisan make:migration:json --file=schema.json --disableundo
这将防止创建撤销文件。
验证
要检查您的json文件是否具有有效的JSON语法和模式验证(列类型定义和列类型修饰符检查)
php artisan make:migration:json --file=schema.json --validate
注意:这不会生成任何迁移文件,只会检查您是否误拼了任何字段模式定义
JSON文件示例
使用表名或迁移名
您可以使用表名或使用Extended Generators将理解的迁移名。
例如
{ "users": { "email": "string:unique", "password": "string:index" } }
等同于
{ "create_users_table": { "email": "string:unique", "password": "string:index" } }
将所有内容组合起来
现在您可以尽情地定义整个数据库模式,并享受到在单个文件中查看所有内容的便利。如您所见,我们可以使用--undo
来删除上一次命令生成的所有文件,然后编辑我们的JSON文件,使用--validate
来验证语法,然后再重新生成所有内容。一句话:太棒了!哇哦。
{ "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 }