Php Laravel迁移-删除列
我需要从我的数据库表Php Laravel迁移-删除列,php,laravel,symfony,doctrine-orm,Php,Laravel,Symfony,Doctrine Orm,我需要从我的数据库表clients中删除列UserDomainName 首先,我通过执行编写器要求编写器/dbal安装编写器/dbal,然后执行编写器更新,如中所述 然后,我创建了要用于删除列的迁移: php artisan make:migration remove_user_domain_name_from_clients --table=clients 我添加了Schema::dropColumn('UserDomainName')到down()方法: <?php use Ill
clients
中删除列UserDomainName
首先,我通过执行编写器要求编写器/dbal
安装编写器/dbal
,然后执行编写器更新
,如中所述
然后,我创建了要用于删除列的迁移:
php artisan make:migration remove_user_domain_name_from_clients --table=clients
我添加了Schema::dropColumn('UserDomainName')编码>到down()
方法:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemoveDomainName extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('clients', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('clients', function (Blueprint $table) {
Schema::dropColumn('UserDomainName');
});
}
}
执行php artisan migrate
后,不会删除任何列。
如果我再次执行它,则不会得到任何要迁移的内容。
向下
函数用于回滚,您必须在向上函数中添加此下拉列
,因为这是运行迁移时要执行的操作
因此,在up
函数中应该有:
Schema::table('clients', function (Blueprint $table) {
$table->dropColumn('UserDomainName');
});
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('UserDomainName');
});
}
在down
函数中,您应该执行相反的操作,将列添加回:
Schema::table('clients', function (Blueprint $table) {
$table->string('UserDomainName');
});
这样,您就可以始终返回到迁移中的任何点。对于dropColumn,您可以这样做 在向下功能中,应该有:
Schema::table('clients', function (Blueprint $table) {
$table->dropColumn('UserDomainName');
});
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('UserDomainName');
});
}
然后跑
php artisan迁移:回滚
就是这样。我认为
dropIfExists
函数只适用于整个表。谢谢!我试过了,现在调用了undefined方法illumb\Database\Schema\MySqlBuilder::dropColumn()我尝试解决这个问题,然后再次尝试您的解决方案。您使用过我发布的代码吗?因为在您的示例中,您在模式
类上静态使用函数,这将不起作用。不要使用会删除整个表的dropIfExists('column')
@杰夫