1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Schema; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Database\Migrations\Migration; |
6
|
|
|
|
7
|
|
|
class CreatePermissionTables extends Migration |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Run the migrations. |
11
|
|
|
*/ |
12
|
|
|
public function up() |
13
|
|
|
{ |
14
|
|
|
$tableNames = config('permission.table_names'); |
15
|
|
|
|
16
|
|
|
Schema::create($tableNames['permissions'], function (Blueprint $table) { |
17
|
|
|
$table->increments('id'); |
18
|
|
|
$table->string('name'); |
19
|
|
|
$table->string('guard_name'); |
20
|
|
|
$table->timestamps(); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) { |
24
|
|
|
$table->increments('id'); |
25
|
|
|
$table->string('name'); |
26
|
|
|
$table->string('guard_name'); |
27
|
|
|
$table->timestamps(); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) { |
31
|
|
|
$table->unsignedInteger('permission_id'); |
32
|
|
|
$table->morphs('model'); |
33
|
|
|
|
34
|
|
|
$table->foreign('permission_id') |
35
|
|
|
->references('id') |
36
|
|
|
->on($tableNames['permissions']) |
37
|
|
|
->onDelete('cascade'); |
38
|
|
|
|
39
|
|
|
$table->primary(['permission_id', 'model_id', 'model_type']); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) { |
43
|
|
|
$table->unsignedInteger('role_id'); |
44
|
|
|
$table->morphs('model'); |
45
|
|
|
|
46
|
|
|
$table->foreign('role_id') |
47
|
|
|
->references('id') |
48
|
|
|
->on($tableNames['roles']) |
49
|
|
|
->onDelete('cascade'); |
50
|
|
|
|
51
|
|
|
$table->primary(['role_id', 'model_id', 'model_type']); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
55
|
|
|
$table->unsignedInteger('permission_id'); |
56
|
|
|
$table->unsignedInteger('role_id'); |
57
|
|
|
|
58
|
|
|
$table->foreign('permission_id') |
59
|
|
|
->references('id') |
60
|
|
|
->on($tableNames['permissions']) |
61
|
|
|
->onDelete('cascade'); |
62
|
|
|
|
63
|
|
|
$table->foreign('role_id') |
64
|
|
|
->references('id') |
65
|
|
|
->on($tableNames['roles']) |
66
|
|
|
->onDelete('cascade'); |
67
|
|
|
|
68
|
|
|
$table->primary(['permission_id', 'role_id']); |
69
|
|
|
|
70
|
|
|
app('cache')->forget('spatie.permission.cache'); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Reverse the migrations. |
76
|
|
|
*/ |
77
|
|
|
public function down() |
78
|
|
|
{ |
79
|
|
|
$tableNames = config('permission.table_names'); |
80
|
|
|
|
81
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
82
|
|
|
Schema::drop($tableNames['model_has_roles']); |
83
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
84
|
|
|
Schema::drop($tableNames['roles']); |
85
|
|
|
Schema::drop($tableNames['permissions']); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|