Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel迁移-删除列_Php_Laravel_Symfony_Doctrine Orm - Fatal编程技术网

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')
@杰夫