Laravel 迁移检查表是否存在
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
Laravel 迁移检查表是否存在的更多相关文章
- 小程序实现sql插入语句转换成Laravel迁移语句
sql的插入语句长这样: INSERT INTO `media` (`MediaID`, `type`, `filename`, `title`) VALUES (1, 'word', 'word1. ...
- 关于Laravel 迁移数据库的问题
今天在Homestead 中用 php artisan migrate 迁移数据库时出现了拒绝的情况: ***之后发现只要修改项目文件夹下面的database.php 和.env 文件中的数据库配置, ...
- laravel 迁移文件中修改含有enum字段的表报错解决方法
解决方法: 在迁移文件中up方法最上方加上下面这一行代码即可: Schema::getConnection()->getDoctrineSchemaManager()->getDataba ...
- laravel 迁移枚举
$table->enum('type', ['replace', 'warning'])->comment('类型');
- laravel迁移文件中字段方法对应的数据库类型
/* *Blueprint类中的方法方法 <-> 数据库数据类型 * */ // 数字 increments();// int(10) unsigned primarykey auto_i ...
- Laravel学习笔记(四)数据库 数据库迁移案例
创建迁移 首先,让我们创建一个MySql数据库“Laravel_db”.接下来打开app/config目录下的database.php文件.请确保default键值是mysql: return arr ...
- Laravel学习笔记(三)数据库 数据库迁移
该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...
- laravel_5《数据库迁移》
Laravel鼓励敏捷.迭代的开发方式,我们没指望在第一次就获得所有正确的.相反,我们编写代码.测试和与我们的最终用户进行交互,并完善我们的理解. 对于工作,我们需要一个配套的实践集.我们使用像sub ...
- Laravel 生成migration ,boolean字段字段转为tinyInteger
Schema::create('consults', function (Blueprint $table) { $table->increments('id'); $table->str ...
随机推荐
- Day 19:面向对象【类方法】静态属性/静态属性/类方法
静态属性 @property class Mom: gender = "woman" def __init__(self,name,weight): self.name = n ...
- cdh5.47 上配置flume
flume 配置文件 # Define a memory channel called ch1 on agent1agent1.channels.ch1.type = memoryagent1.cha ...
- java-day02
数据类型自动转换 要求:数据范围从小到大 数据类型强制类型转换 格式:范围小的数据类型 范围小的变量名 = (范围小的数据类型)原范围大的数据 注意事项: 1.可以会造成数据溢出或者是精度损失. 2. ...
- zabbix--添加用户
用户和用户组 概述 Zabbix 中的所有用户都通过 Web 前端去访问 Zabbix 应用程序.并为每个用户分配唯一的登陆名和密码. 所有用户的密码都被加密并储存于 Zabbix 数据库中.用户 ...
- 第五篇 scrapy安装及目录结构,启动spider项目
实际上安装scrapy框架时,需要安装很多依赖包,因此建议用pip安装,这里我就直接使用pycharm的安装功能直接搜索scrapy安装好了. 然后进入虚拟环境创建一个scrapy工程: (third ...
- github合并分支到master
(1)切换到master分支 git checkout master (2) 将backup分支的代合并到master git merge backup (3) 查看状态 git status (4) ...
- Vue.js 监听属性
demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf- ...
- WordPress .gitignore
# ----------------------------------------------------------------- # .gitignore for WordPress @salc ...
- Kotlin Download
{ https://github.com/JetBrains/kotlin/releases/tag/v1.3.50 }
- thinkphp 静态缓存
要使用静态缓存功能,需要开启HTML_CACHE_ON参数,并且使用HTML_CACHE_RULES配置参数设置静态缓存规则文件 . 大理石构件厂家 虽然也可以在应用配置文件中定义静态缓存规则,但是建 ...