作者 ZhengBing He

添加外键约束

  1 +<?php
  2 +
  3 +use Illuminate\Database\Migrations\Migration;
  4 +use Illuminate\Database\Schema\Blueprint;
  5 +use Illuminate\Support\Facades\Schema;
  6 +
  7 +class WorkOrderLogsAddForeignId extends Migration
  8 +{
  9 + /**
  10 + * Run the migrations.
  11 + *
  12 + * @return void
  13 + */
  14 + public function up()
  15 + {
  16 + Schema::table('gl_work_order_logs', function (Blueprint $table) {
  17 + // 字段已存在,给字段添加外键约束
  18 + $table->foreign('work_order_id')
  19 + ->references('id')->on('gl_work_orders')
  20 + ->onDelete('cascade'); // 设置级联删除
  21 + });
  22 + }
  23 +
  24 + /**
  25 + * Reverse the migrations.
  26 + *
  27 + * @return void
  28 + */
  29 + public function down()
  30 + {
  31 + Schema::table('gl_work_order_logs', function (Blueprint $table) {
  32 + // 删除外键约束
  33 + $table->dropForeign(['work_order_id']);
  34 + });
  35 + }
  36 +}