forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2019_09_20_122431_add_references.php
40 lines (31 loc) · 1.23 KB
/
2019_09_20_122431_add_references.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddReferences extends Migration
{
public function up()
{
Schema::table('topics', function (Blueprint $table) {
// 当 user_id 对应的 users 表数据被删除时,删除词条
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::table('replies', function (Blueprint $table) {
// 当 user_id 对应的 users 表数据被删除时,删除此条数据
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// 当 topic_id 对应的 topics 表数据被删除时,删除此条数据
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
});
}
public function down()
{
Schema::table('topics', function (Blueprint $table) {
// 移除外键约束
$table->dropForeign(['user_id']);
});
Schema::table('replies', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['topic_id']);
});
}
}