【问题标题】:Check If a Column Exists in Laravel Migration File检查 Laravel 迁移文件中是否存在列
【发布时间】:2019-06-08 03:11:27
【问题描述】:

我已经有一个表名table_one. 现在我想再添加两列。到目前为止一切正常。但在我的方法中,我想检查我的表中是否存在列,例如dropIfExists('table').

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('table_one', function (Blueprint $table) {
        $table->string('column_one')->nullable();
        $table->string('column_two')->nullable();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('table_one', function (Blueprint $table) {
        // in here i want to check column_one and column_two exists or not
        $table->dropColumn('column_one');
        $table->dropColumn('column_two');
    });
}

【问题讨论】:

    标签: php sql laravel


    【解决方案1】:

    你需要这样的东西

      public function down()
        {
            if (Schema::hasColumn('users', 'phone'))
            {
                Schema::table('users', function (Blueprint $table)
                {
                    $table->dropColumn('phone');
                });
            }
        }
    

    【讨论】:

    • 谢谢。您的解决方案运行良好。但我不喜欢这种方法。我想要 dropIfExists('column') 之类的东西。
    • 别忘了安装学说/dbal 包。
    【解决方案2】:

    只需将架构分成两个调用

    public function up()
    {
        Schema::table('table_one', function (Blueprint $table) {
            $table->dropColumn(['column_one', 'column_two']);
        });
    
        Schema::table('table_one', function (Blueprint $table) {
            $table->string('column_one')->nullable();
            $table->string('column_two')->nullable();
        });
    }
    

    【讨论】:

    • 这将导致这些列中的所有数据也被删除!非常危险的代码!
    【解决方案3】:

    您可以创建自己的“dropColumnIfExists()”函数来检查列是否存在,然后将其删除:

    function myDropColumnIfExists($myTable, $column)
    {
        if (Schema::hasColumn($myTable, $column)) //check the column
        {
            Schema::table($myTable, function (Blueprint $table)
            {
                $table->dropColumn($column); //drop it
            });
        }
    
    }
    

    并在 'down()' 函数上使用它,如下所示:

    public function down()
    {
        myDropColumnIfExists('table_one', 'column_two');
        myDropColumnIfExists('table_one', 'column_one');
    }
    

    【讨论】:

    • 请始终提供一些解释,而不仅仅是一段代码。
    【解决方案4】:

    如果你真的想在 Schema::table 闭包中使用它,这是唯一的巧妙方法......你需要向 Blueprint 添加几个方法,或者在每次迁移中使用这个样板。 .. 它不是很漂亮,但是一旦它在那里,您可以定义任意数量的条件添加和删除,每个只使用 1 行。

    return new class extends Migration {
        public function up() {
    
            Schema::table('tblAnimal', function (Blueprint $table) {
                $exists = function (string $column) use ($table) {
                    return (Schema::hasColumn($table->getTable(), $column));
                };
                $addUnlessExists = function (string $type, string $name, array $parameters = [])
                    use ($table, $exists) {
                        return $exists($name) ? null : $table->addColumn($type, $name, $parameters);
                    };
                $dropIfExists = function (string $column) use ($table, $exists) {
                    return $exists($column) ? $table->dropColumn($column) : null;
                };
    
                $dropIfExists('column_name');
                $addUnlessExists('integer', 'int_column');
                # ...
            });
    

    【讨论】:

      【解决方案5】:

      要动态检查您的表格列,您可以尝试以下操作:

      public function dropIfExists($table, $column)
      {
          if (Schema::hasColumn($table, $column)) //check the column
          {
              Schema::table($table, function (Blueprint $table) use ($column)
              {
                  $table->dropColumn($column); //drop it
              });
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2023-04-10
        • 1970-01-01
        • 2021-10-28
        • 1970-01-01
        • 2019-07-05
        • 2019-01-12
        • 1970-01-01
        • 2017-01-02
        • 2015-06-14
        相关资源
        最近更新 更多