php - 检查 Laravel 迁移文件中是否存在列
问题描述
我已经有了一个表名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');
});
}
}
推荐阅读
- python - ImportError en el jupyter notebook 尽管安装了名为的库
- qt - 如何在 Qt 中使用 QImage 更改图像中的文本(前景)和背景颜色?
- python - 张量必须来自与张量相同的图
- node.js - 如何使用多个 POST 端点构建 REST api
- cucumber - Capybara升级后两次启动puma
- php - Apache .htaccess 没有正确执行
- nginx - 403 Forbidden - Nginx - oracle云实例
- react-native - React Navigation (V2):如何在 Drawer Navigator 中设置 Stack Navigator 的 Icon 和 label?
- html - 在使用 HTML 实体时,它还可以翻译 &
- java - 如何准确扫描android中所有连接到wifi的设备的IP和Mac地址?