如果我有一些模型,具有非默认功能(如软删除或自定义ID名称)并希望从中创建一个迁移文件,该文件中包含所有这些属性,该怎么办?所以这是我的模型:
class Test extends Model
{
use SoftDeletes;
protected $primaryKey = 'test_id';
protected $table = 'my_flights';
protected $dates = ['deleted_at'];
}
我希望我的Migration文件基于它。但当我使用命令php artisan make:migration create_test(s)_table
(我尝试了test
和tests
)也参与--create
或--table
我得到了迁移文件,因为没有任何模型:
class CreateTestTable extends Migration
{
public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
} ...
在正常开发中,迁移先于模型详细信息。迁移包含模式定义,而不是模型。 Eloquent模型遵循活动记录模式,不包含列的类型定义。如果遵循命名约定,则无需在模型中指定有关表或列的任何内容。
make:migration命令没有任何内容可以从现有模型或数据库表中提取任何内容。有一个包支持从现有数据库模式创建迁移:https://github.com/Xethron/migrations-generator,但不支持从模型本身创建。