Schema::hasTable('TableName'); //检查表释放存在
Schema::hasColumn('tableName', 'columeName'); //检查表是否存在某个字段

eg. alert_test_add_timesteamps_field.php 文件,添加updated_atcreated_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
  1. Is there any way to detect if a database table exists with Laravel

Laravel 迁移检查表是否存在的更多相关文章

  1. 小程序实现sql插入语句转换成Laravel迁移语句

    sql的插入语句长这样: INSERT INTO `media` (`MediaID`, `type`, `filename`, `title`) VALUES (1, 'word', 'word1. ...

  2. 关于Laravel 迁移数据库的问题

    今天在Homestead 中用 php artisan migrate 迁移数据库时出现了拒绝的情况: ***之后发现只要修改项目文件夹下面的database.php 和.env 文件中的数据库配置, ...

  3. laravel 迁移文件中修改含有enum字段的表报错解决方法

    解决方法: 在迁移文件中up方法最上方加上下面这一行代码即可: Schema::getConnection()->getDoctrineSchemaManager()->getDataba ...

  4. laravel 迁移枚举

    $table->enum('type', ['replace', 'warning'])->comment('类型');

  5. laravel迁移文件中字段方法对应的数据库类型

    /* *Blueprint类中的方法方法 <-> 数据库数据类型 * */ // 数字 increments();// int(10) unsigned primarykey auto_i ...

  6. Laravel学习笔记(四)数据库 数据库迁移案例

    创建迁移 首先,让我们创建一个MySql数据库“Laravel_db”.接下来打开app/config目录下的database.php文件.请确保default键值是mysql: return arr ...

  7. Laravel学习笔记(三)数据库 数据库迁移

    该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...

  8. laravel_5《数据库迁移》

    Laravel鼓励敏捷.迭代的开发方式,我们没指望在第一次就获得所有正确的.相反,我们编写代码.测试和与我们的最终用户进行交互,并完善我们的理解. 对于工作,我们需要一个配套的实践集.我们使用像sub ...

  9. Laravel 生成migration ,boolean字段字段转为tinyInteger

    Schema::create('consults', function (Blueprint $table) { $table->increments('id'); $table->str ...

随机推荐

  1. Zabbix监控搭建

    目录 Zabbix概述        zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 ( 基于 GPL V2 )zabbix由 2 部分构成,zabbix ...

  2. org.apache.hadoop.hbase.master.HMasterCommandLine: Master exiting java.lang.RuntimeException: HMaster Aborted

    前一篇的问题解决了,是 hbase 下面lib 包的jar问题,之前写MR的时候加错了包,替换掉了原来的包后出现另一问题:@ubuntu:/home/hadoop/hbase-0.94.6-cdh4. ...

  3. 无法CREATE UNIQUE INDEX;找到重复的关键字

  4. uuencode - 对二进制文件编码

    总览 (SYNOPSIS) uuencode [-m] [ file ] name uudecode [-o outfile] [ file ]... 描述 (DESCRIPTION) Uuencod ...

  5. c++内存相关函数

    memset void *memset(void *s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ...

  6. Bootstrap4入门

    基础样式 颜色 文字颜色以.text-*开头 背景颜色.bg-* primary / seconday / success / danger / warning / info / muted / wh ...

  7. 57 CUDA 编程入门

    0 引言 由于毕设用到了Marvin,采用的是CUDA框架作为加速器,正好借此学习一下CUDA编程的一些基本知识. 各个版本的cuda的下载链接如下. https://developer.nvidia ...

  8. NX二次开发-C++的vector排序去重用法

    #include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...

  9. (转)Java安全通信:HTTPS与SSL

    转:http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html 1. HTTPS概念 1)简介 HTTPS(全称:Hyperte ...

  10. 开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误

    开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误, 调试后发现是由于写的SQL语句里列的数目和后面的值 ...