简体   繁体   中英

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

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();
        });
    }  ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM