问题
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