How to make a Migration file based on a model in Laravel

天大地大妈咪最大 提交于 2021-01-29 07:13:20

问题


What if I have some model, with non-default features (like soft delete or custom id name) and want to create a Migration file from it, which would have all these properties in it? So here is my model:

class Test extends Model
{
   use SoftDeletes;

   protected $primaryKey = 'test_id';
   protected $table = 'my_flights';
   protected $dates = ['deleted_at'];
}

And I want my Migration file to be based on it. But when I use command php artisan make:migration create_test(s)_table (I tried both test and tests) also with parameters --create or --table I get the Migration file as there was not any model:

class CreateTestTable extends Migration
{
    public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }  ...

回答1:


In normal development, the migration precedes the model details. The migration contains the schema definitions, not the model. Eloquent models follow the active record pattern and do not contain type definitions for columns. If following naming conventions, you do not need to specify anything about your table or columns in the model.

Nothing about the make:migration command will pull anything from existing models or database tables. There is a package out there to support creating migrations from an existing database schema: https://github.com/Xethron/migrations-generator but not from a model itself.



来源:https://stackoverflow.com/questions/52339124/how-to-make-a-migration-file-based-on-a-model-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!