legodion / lucid
在 Laravel 模型内部声明数据库迁移和工厂定义。
1.0.11
2022-03-26 12:35 UTC
Requires
- doctrine/dbal: ^2.0|^3.0
- laravel/framework: ^7.0|^8.0|^9.0
README
此包允许您在您的 Laravel 模型内部声明数据库迁移和工厂定义。
运行 lucid:migrate
命令将自动将您在 migration
方法中做出的任何更改应用于数据库,通过 Doctrine DBAL。如果您使用 HasNewFactory
特性和 definition
方法,它将使用 definition
方法返回的数组进行填充,当使用 -s
选项时。
lucid:migrate
命令还会先运行基于文件的(传统)Laravel 迁移,然后运行您的模型方法迁移。如果您需要按特定顺序运行基于模型的迁移,您可以在模型中添加一个具有整数值的 $migrationOrder
属性(默认为 0
)。
安装
通过 Composer 需求此包
composer require legodion/lucid
用法
使用 HasNewFactory
特性,并在您的模型中声明 migration
和 definition
方法
use Faker\Generator; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; use Legodion\Lucid\Traits\HasNewFactory; class MyModel extends Model { use HasNewFactory; protected $guarded = []; protected $migrationOrder = 1; // optional public function migration(Blueprint $table) { $table->id(); $table->string('name'); $table->timestamp('created_at')->nullable(); $table->timestamp('updated_at')->nullable(); } public function definition(Generator $faker) { return [ 'name' => $faker->name(), 'created_at' => $faker->dateTimeThisMonth(), ]; } }
命令
迁移
将 migration
方法内的更改应用于您的数据库
php artisan lucid:migrate {--f|--fresh} {--s|--seed}
使用 -f
选项进行全新迁移,并/或使用 -s
选项在之后运行种子器。
创建模型
创建一个包含 migration
和 definition
方法的模型
php artisan lucid:model {name} {--r|--resource}
使用 -r
选项同时创建模型的 Laravel Nova 资源。
创建 Nova 资源
创建不带所有注释的 Laravel Nova 资源
php artisan lucid:resource {name} {--m|--model}
使用 -m
选项同时创建 Nova 资源对应的模型。