laravel-learn-bbs/database/migrations/2018_06_02_215326_add_references.php

42 lines
1.1 KiB
PHP
Raw Normal View History

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddReferences extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('topics', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::table('replies', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('topics', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::table('replies', function (Blueprint $table) {
$table->dropForeign(['user_id', 'topic_id']);
});
}
}