在使用 php artisan make:migration 创建migration时,可用 --path 指定创建migration文件的路径,

如果在执行的 php artisan migrate 命令,出现找不到对应class,

可以用 php artisan clear-compiled php artisan optimize 命令 移除编译过的类文件和优化,

命令详细的参数可以通过 php artisan help [command] 命令来查看。

在migration中创建表以及数据库相关的,使用到原生的sql脚本时,可以用调用DB类中的unprepared方法实现:

 <?php

 use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB; class CreateItemTempTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('item_temp', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci'; $table->string('o_id',50)->notNull()->comment('订单编号');
$table->string('outer_oi_id',50)->default(null)->comment('子订单号');
$table->string('sku_id')->notNull()->comment('商家SKU');
$table->string('name',100)->default(null)->comment('商品名称');
$table->decimal('amount',12,2)->default(0.00)->comment('应付金额');
$table->decimal('base_price',12,2)->default(0.00)->comment('基本价(拍下价格)');
$table->decimal('price',12,2)->default(0.00)->comment('');
$table->string('properties_value',100)->default(null)->comment('属性');
$table->integer('qty')->default(0)->comment('购买数量');
$table->string('raw_so_id',50)->default(null)->comment('');
$table->string('refund_id',50)->default(null)->comment('退货ID');
$table->integer('refund_qty')->default(0)->comment('退货数量');
$table->string('refund_status',40)->default(null)->comment('退货状态');
$table->string('shop_sku_id')->default(null)->comment('网站对应的自定义SKU编号'); }); DB::unprepared('
CREATE TRIGGER `sync_to_item_table`
BEFORE INSERT
ON `item_temp` FOR EACH ROW
BEGIN
IF NOT EXISTS(SELECT id FROM `item` WHERE o_id = new.o_id AND sku_id = new.sku_id ) THEN
INSERT INTO `item`(o_id,outer_oi_id,sku_id,name,amount,base_price,price,properties_value,qty,raw_so_id,refund_id,refund_qty,refund_status,shop_sku_id)
VALUES(new.o_id,new.outer_oi_id,new.sku_id,new.name,new.amount,new.base_price,new.price,new.properties_value,new.qty,new.raw_so_id,new.refund_id,new.refund_qty,new.refund_status,new.shop_sku_id);
ELSE
UPDATE `item` SET
outer_oi_id = new.outer_oi_id,
name = new.name,
amount = new.amount,
base_price = new.base_price,
price = new.price,
properties_value = new.properties_value,
qty = new.qty,
raw_so_id = new.raw_so_id,
refund_id = new.refund_id,
refund_qty = new.refund_qty,
refund_status = new.refund_status,
shop_sku_id = new.shop_sku_id
WHERE o_id = new.o_id AND sku_id = new.sku_id;
END IF;
END
'); DB::unprepared('
CREATE EVENT IF NOT EXISTS `clear_item_temp`
ON SCHEDULE
EVERY 1 DAY
DO TRUNCATE `item_temp`
');
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('item_temp');
DB::unprepared('DROP TRIGGER IF EXISTS `sync_to_item_table`');
DB::unprepared('DROP EVENT IF EXISTS `clear_item_temp`'); }
}

Laravel使用笔记 —— migration的更多相关文章

  1. Laravel学习笔记(三)--在CentOS上配置Laravel

    在Laravel框架上开发了几天,不得不说,确实比较优雅,处理问题逻辑比较清楚.     今天打算在CentOS 7上配置一个Laravel,之前都是在本机上开发,打算实际配置一下.     1)系统 ...

  2. Laravel学习笔记之Session源码解析(上)

    说明:本文主要通过学习Laravel的session源码学习Laravel是如何设计session的,将自己的学习心得分享出来,希望对别人有所帮助.Laravel在web middleware中定义了 ...

  3. Laravel学习笔记之PHP反射(Reflection) (上)

    Laravel学习笔记之PHP反射(Reflection) (上) laravel php reflect 2.1k 次阅读  ·  读完需要 80 分钟 3 说明:Laravel中经常使用PHP的反 ...

  4. laravel安装 笔记

    http://laod.cn/hosts/2015-google-hosts.html 谷歌FQIP laravel安装和设置流程 1安装composer , VirtualBox和Vagrant 下 ...

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

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

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

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

  7. Laravel学习笔记(六)数据库 数据库填充

    数据库驱动的应用程序往往需要预先填充数据到数据库,以便进行测试和演示. 什么是种子数据 种子数据就是必须要加载了应用程序才能正常运行的数据.大多数应用程序需要在开发.测试和生产中加载一些参考数据. 一 ...

  8. 慕客网laravel学习笔记

    session中set方法使用 Session::set('user.username.age','18')嵌套使用得出$user = ['username'=>['age'=>18]]; ...

  9. laravel整理笔记(一)

    安装laravel5.8.3需要的环境 PHP >= 7.1.3 PHP OpenSSL 扩展 PHP PDO 扩展 PHP Mbstring 扩展 PHP Tokenizer 扩展 PHP X ...

随机推荐

  1. spring框架学习(三)

    一.Spring自动组件扫描 Spring 提供组件扫描(component scanning)功能.它能从指定的classpath里自动扫描.侦测和实例化具有特定注解的组件. 基本的注解是@Comp ...

  2. Unity3D 响应摇杆

    if (Input.GetKeyUp(KeyCode.Joystick1Button0)) { Debug.Log("Joystick1Button0"); } if (Input ...

  3. js_继承

    一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 复制代码代码如下: <SPAN style="<SPAN style="FONT- ...

  4. [2013-08-01]window.open

    经常上网的朋友可能会到过这样一些网站,一进入首页立刻会弹出一个窗口,或者按一个连接或按钮弹出,通常在这个窗口里会显示一些注意事项.版权信息.警告.欢迎光顾之类的话或者作者想要特别提示的信息.其实制作这 ...

  5. lockf

    lockf( fd, mode, size ); mode 为 1 时表示加锁,为 0 时表示解锁. #include<stdio.h> #include<unistd.h> ...

  6. sh4.case语句

    case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构.case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...

  7. RSA原理及生成步骤

    摘自:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html(可到原网址查看秘钥生成原理) RSA算法原理(一) 因为它是 ...

  8. PLSQL note

    sql%count 最近一次sql执行的件数SUBSTR(string , int i) // i番目から最後までの文字列を切り取るSUBSTR(string , int i, int j) // i ...

  9. Java笔记:异常

    Exception 类的层次 所有的异常类是从 java.lang.Exception 类继承的子类. Exception 类是 Throwable 类的子类.除了Exception类外,Throwa ...

  10. linux的安装

    在CentOS 7中提供了两种桌面"GNOME DESKTOP" 和 "KDE Plasa Workspaces",我们以安装"GNOME DESKT ...