gpt4 book ai didi

php - 检查 Laravel 迁移文件中是否存在列

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:29 25 4
gpt4 key购买 nike

我已经有了一个表名 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');
});
}

最佳答案

你需要这样的东西

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

关于php - 检查 Laravel 迁移文件中是否存在列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54176290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com