从现有数据库生成 Laravel 迁移,包括索引和外键!
请注意,Laravel 4 Generator 编辑已移至https://github.com/xethron/Laravel-4-Generators.git
以更新兼容性。
推荐的安装方法是通过 Composer:
composer require --dev " xethron/migrations-generator "
在 Laravel 5.5 中,服务提供商将自动注册。
在旧版本的框架中,编辑config/app.php
并将其添加到提供程序部分:
Way Generators GeneratorsServiceProvider::class,
Xethron MigrationsGenerator MigrationsGeneratorServiceProvider::class,
如果您只想将此库用于开发,您可以将以下代码添加到您的app/Providers/AppServiceProvider.php
文件中的register()
方法中:
public function register ()
{
if ( $ this -> app -> environment () !== ' production ' ) {
$ this -> app -> register ( Way Generators GeneratorsServiceProvider::class);
$ this -> app -> register ( Xethron MigrationsGenerator MigrationsGeneratorServiceProvider::class);
}
// ...
}
笔记:
feature/laravel-five-stable
是从way/generators
3.0.3
分叉出来的,并为 Laravel 5.0
做好了准备。 Jeffrey Way 已停止对 Laravel 5 的支持,因此其他artisan generate:
命令可能尚未与5.0
兼容。研究artisan make:
替代品的命令,为 Laravel 做出贡献以扩展生成支持,或修复它并向jamisonvalenta/feature/laravel-five-stable
提交 PR。 运行以下作曲家命令:
composer require --dev " xethron/migrations-generator:~1.3.0 "
接下来,添加以下服务提供商:
' WayGeneratorsGeneratorsServiceProvider ' ,
' XethronMigrationsGeneratorMigrationsGeneratorServiceProvider ' ,
你就准备好了。要仔细检查其是否正常工作,请运行php artisan
,然后查找命令migrate:generate
要从数据库生成迁移,您需要在 Laravel 的配置中设置数据库。
运行php artisan migrate:generate
为所有表创建迁移,或者您可以使用php artisan migrate:generate table1,table2,table3,table4,table5
指定要生成的表。您还可以使用--ignore="table3,table4,table5"
忽略表
Laravel Migrations Generator 将首先生成所有表、列和索引,然后设置所有外键约束。因此,请确保包含外键中列出的所有表,以便在创建外键时它们都存在。
如果您不使用默认连接,您还可以通过--connection="connection_name"
指定连接名称
运行php artisan help migrate:generate
以获取选项列表。
查看 Chung Tran 的博客文章,了解快速的分步介绍:从 Laravel 4 中的现有数据库生成迁移
Laravel 迁移生成器的变更日志
感谢 Jeffrey Way 提供的令人惊叹的 Laravel-4-Generators 包。这个包很大程度上取决于他的工作。
伯恩哈德·布雷滕巴赫 (@BBreyten)
Laravel Migrations Generator 是根据 MIT 许可证授权的开源软件