Laravel:如何创建用于从 table 中删除多列的迁移文件?
Laravel: How to create migration file for drop multiple columns from a table?
删除列时,我使用以下命令创建迁移文件,
php artisan make:migration drop_institue_id_column_from_providers
和迁移文件:
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
});
}
如果要drop多列,可以这样写迁移文件,
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
$table->dropColumn('institue_name');
$table->dropColumn('institue_address');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
$table->char('institue_name');
$table->char('institue_address');
});
}
但是创建迁移文件的命令是什么?谢谢。
我认为这只是一个关于迁移命名约定的问题 class。
如果是这样,那么 file\class 叫什么并不重要,只要它能让您清楚地了解它的作用即可。
所以它可能是这样的:
php artisan make:migration drop_institue_columns_from_providers
当您执行 php artisan make:migration
命令时,它只会创建一个 class,您可以在其中指定可以执行的迁移。您可以对一个 table 进行多次修改,甚至可以对多个 table 进行修改。所以把你所有的代码放在一个迁移文件中就可以了。
创建迁移文件运行:
php artisan make: migration your_migration_file_name_here
然后你可以通过传递数组
来删除多列
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id', 'institue_name', 'institue_address');
});
删除列时,我使用以下命令创建迁移文件,
php artisan make:migration drop_institue_id_column_from_providers
和迁移文件:
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
});
}
如果要drop多列,可以这样写迁移文件,
public function up()
{
Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id');
$table->dropColumn('institue_name');
$table->dropColumn('institue_address');
});
}
public function down()
{
Schema::table('providers', function (Blueprint $table)
{
$table->integer('institue_id')->unsigned();
$table->char('institue_name');
$table->char('institue_address');
});
}
但是创建迁移文件的命令是什么?谢谢。
我认为这只是一个关于迁移命名约定的问题 class。
如果是这样,那么 file\class 叫什么并不重要,只要它能让您清楚地了解它的作用即可。
所以它可能是这样的:
php artisan make:migration drop_institue_columns_from_providers
当您执行 php artisan make:migration
命令时,它只会创建一个 class,您可以在其中指定可以执行的迁移。您可以对一个 table 进行多次修改,甚至可以对多个 table 进行修改。所以把你所有的代码放在一个迁移文件中就可以了。
创建迁移文件运行:
php artisan make: migration your_migration_file_name_here
然后你可以通过传递数组
来删除多列 Schema::table('providers', function(Blueprint $table) {
$table->dropColumn('institue_id', 'institue_name', 'institue_address');
});