1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
|
6
|
|
|
class CreatePermissionTables extends Migration |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Run the migrations. |
10
|
|
|
* |
11
|
|
|
* @return void |
12
|
|
|
*/ |
13
|
|
|
public function up() |
14
|
|
|
{ |
15
|
|
|
$tableNames = config('permission.table_names'); |
16
|
|
|
|
17
|
|
|
Schema::create($tableNames['permissions'], function (Blueprint $table) { |
18
|
|
|
$table->increments('id'); |
19
|
|
|
$table->string('name'); |
20
|
|
|
$table->string('guard_name'); |
21
|
|
|
$table->timestamps(); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) { |
25
|
|
|
$table->increments('id'); |
26
|
|
|
$table->string('name'); |
27
|
|
|
$table->string('guard_name'); |
28
|
|
|
$table->timestamps(); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
View Code Duplication |
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) { |
|
|
|
|
32
|
|
|
$table->integer('permission_id')->unsigned(); |
33
|
|
|
$table->morphs('model'); |
34
|
|
|
|
35
|
|
|
$table->foreign('permission_id') |
36
|
|
|
->references('id') |
37
|
|
|
->on($tableNames['permissions']) |
38
|
|
|
->onDelete('cascade'); |
39
|
|
|
|
40
|
|
|
$table->primary(['permission_id', 'model_id', 'model_type']); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
View Code Duplication |
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) { |
|
|
|
|
44
|
|
|
$table->integer('role_id')->unsigned(); |
45
|
|
|
$table->morphs('model'); |
46
|
|
|
|
47
|
|
|
$table->foreign('role_id') |
48
|
|
|
->references('id') |
49
|
|
|
->on($tableNames['roles']) |
50
|
|
|
->onDelete('cascade'); |
51
|
|
|
|
52
|
|
|
$table->primary(['role_id', 'model_id', 'model_type']); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
56
|
|
|
$table->integer('permission_id')->unsigned(); |
57
|
|
|
$table->integer('role_id')->unsigned(); |
58
|
|
|
|
59
|
|
|
$table->foreign('permission_id') |
60
|
|
|
->references('id') |
61
|
|
|
->on($tableNames['permissions']) |
62
|
|
|
->onDelete('cascade'); |
63
|
|
|
|
64
|
|
|
$table->foreign('role_id') |
65
|
|
|
->references('id') |
66
|
|
|
->on($tableNames['roles']) |
67
|
|
|
->onDelete('cascade'); |
68
|
|
|
|
69
|
|
|
$table->primary(['permission_id', 'role_id']); |
70
|
|
|
|
71
|
|
|
Cache::forget('spatie.permission.cache'); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Reverse the migrations. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function down() |
81
|
|
|
{ |
82
|
|
|
$tableNames = config('permission.table_names'); |
83
|
|
|
|
84
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
85
|
|
|
Schema::drop($tableNames['model_has_roles']); |
86
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
87
|
|
|
Schema::drop($tableNames['roles']); |
88
|
|
|
Schema::drop($tableNames['permissions']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.