Laravel 迁移检查表是否存在
迁移 是否 存在 laravel
2023-09-11 14:20:09 时间
Schema::hasTable('TableName'); //检查表释放存在
Schema::hasColumn('tableName', 'columeName'); //检查表是否存在某个字段
eg. alert_test_add_timesteamps_field.php
文件,添加updated_at
和created_at
2个字段
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlertTestAddTimesteampsField extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('test', function (Blueprint $table) {
if (!Schema::hasColumn('test', 'updated_at')) {
$table->timestamp('updated_at')->nullable();
}
if (!Schema::hasColumn('test', 'created_at')) {
$table->timestamp('created_at')->nullable();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('test', function (Blueprint $table) {
if (Schema::hasColumn('test', 'updated_at')) {
$table->dropColumn('updated_at');
}
if (Schema::hasColumn('test', 'created_at')) {
$table->dropColumn('created_at');
}
});
}
}
迁移的常用命令(前提:已cd
到项目的根目录下)
php artisan make:migration create_tablename_tables --create=tableName #创建新表时使用
php artisan make:migration alert_tablename_coumn_field --table=tableName #修改表中的字段时使用
php artisan migrate # 执行迁移
php artisan migrate --seed # 执行迁移及生成测试数据
php artisan migrate:rollabck # 回滚上一步迁移
php artisan migrate:refersh # 重新执行迁移
php artisan migrate:refresh --seed # 重新生成表和填充数据
References
相关文章
- git项目裸仓库迁移
- sqlite升级--浅谈Android数据库版本升级及数据的迁移
- oracle迁移postgres之-oracle_fdw
- Veeam Backup & Replication 8 迁移与实例恢复
- 从 MongoDB 迁移到 TDengine 后,成本显著下降
- CV之NS之VGG16:基于Keras框架利用VGG16算法实现图像风格迁移设计(以设计《复仇者联盟3》灭霸图像风格迁移为例)案例应用
- 2天完成17TB数据量迁移,华为云数据库是如何做的?
- 技术实操丨SoundNet迁移学习之由声音分类到语音情感识别
- 【华为云技术分享】鲲鹏开发套件:让应用软件迁移调优So Easy
- 迁移学习(TSRP)《Improving Pseudo Labels With Intra-Class Similarity for Unsupervised Domain Adaptation》
- 迁移学习(EBGAN)《Energy-based Generative Adversarial Network》
- elasticdump备份迁移ES集群数据——筑梦之路
- WSL2之kali从C盘迁移到E盘
- Mysql之数据存储路径迁移
- 使用RMAN Convert Database命令实现跨平台的数据库迁移