1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
|
6
|
|
|
class EntrustSetupTables extends Migration |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Run the migrations. |
10
|
|
|
* |
11
|
|
|
* @return void |
12
|
|
|
*/ |
13
|
|
|
public function up() |
14
|
|
|
{ |
15
|
|
|
// Create table for storing roles |
16
|
|
View Code Duplication |
Schema::create('roles', function (Blueprint $table) { |
|
|
|
|
17
|
|
|
$table->increments('id'); |
18
|
|
|
$table->string('name')->unique(); |
19
|
|
|
$table->string('display_name')->nullable(); |
20
|
|
|
$table->string('description')->nullable(); |
21
|
|
|
$table->timestamps(); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
// Create table for associating roles to users (Many-to-Many) |
25
|
|
View Code Duplication |
Schema::create('role_user', function (Blueprint $table) { |
|
|
|
|
26
|
|
|
$table->integer('user_id')->unsigned(); |
27
|
|
|
$table->integer('role_id')->unsigned(); |
28
|
|
|
|
29
|
|
|
$table->foreign('user_id') |
30
|
|
|
->references('id') |
31
|
|
|
->on('users') |
32
|
|
|
->onUpdate('cascade') |
33
|
|
|
->onDelete('cascade'); |
34
|
|
|
$table->foreign('role_id') |
35
|
|
|
->references('id') |
36
|
|
|
->on('roles') |
37
|
|
|
->onUpdate('cascade') |
38
|
|
|
->onDelete('cascade'); |
39
|
|
|
|
40
|
|
|
$table->primary(['user_id', 'role_id']); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
// Create table for storing permissions |
44
|
|
View Code Duplication |
Schema::create('permissions', function (Blueprint $table) { |
|
|
|
|
45
|
|
|
$table->increments('id'); |
46
|
|
|
$table->string('name')->unique(); |
47
|
|
|
$table->string('display_name')->nullable(); |
48
|
|
|
$table->string('description')->nullable(); |
49
|
|
|
$table->timestamps(); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
// Create table for associating permissions to roles (Many-to-Many) |
53
|
|
View Code Duplication |
Schema::create('permission_role', function (Blueprint $table) { |
|
|
|
|
54
|
|
|
$table->integer('permission_id')->unsigned(); |
55
|
|
|
$table->integer('role_id')->unsigned(); |
56
|
|
|
|
57
|
|
|
$table->foreign('permission_id') |
58
|
|
|
->references('id') |
59
|
|
|
->on('permissions') |
60
|
|
|
->onUpdate('cascade') |
61
|
|
|
->onDelete('cascade'); |
62
|
|
|
$table->foreign('role_id') |
63
|
|
|
->references('id') |
64
|
|
|
->on('roles') |
65
|
|
|
->onUpdate('cascade') |
66
|
|
|
->onDelete('cascade'); |
67
|
|
|
|
68
|
|
|
$table->primary(['permission_id', 'role_id']); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Reverse the migrations. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function down() |
78
|
|
|
{ |
79
|
|
|
Schema::drop('permission_role'); |
80
|
|
|
Schema::drop('permissions'); |
81
|
|
|
Schema::drop('role_user'); |
82
|
|
|
Schema::drop('roles'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.