1
1
<?php
2
2
3
- use Illuminate \Database \Migrations \Migration ;
4
- use Illuminate \Database \Schema \Blueprint ;
5
3
use Illuminate \Support \Facades \Schema ;
6
- use Spatie \Permission \PermissionRegistrar ;
4
+ use Illuminate \Database \Schema \Blueprint ;
5
+ use Illuminate \Database \Migrations \Migration ;
7
6
8
- class CreatePermissionTables extends Migration
7
+ return new class extends Migration
9
8
{
10
9
/**
11
10
* Run the migrations.
12
- *
13
- * @return void
14
11
*/
15
- public function up ()
12
+ public function up (): void
16
13
{
14
+ $ teams = config ('permission.teams ' );
17
15
$ tableNames = config ('permission.table_names ' );
18
16
$ columnNames = config ('permission.column_names ' );
19
- $ teams = config ('permission.teams ' );
17
+ $ pivotRole = $ columnNames ['role_pivot_key ' ] ?? 'role_id ' ;
18
+ $ pivotPermission = $ columnNames ['permission_pivot_key ' ] ?? 'permission_id ' ;
20
19
21
20
if (empty ($ tableNames )) {
22
21
throw new \Exception ('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again. ' );
@@ -50,67 +49,68 @@ public function up()
50
49
}
51
50
});
52
51
53
- Schema::create ($ tableNames ['model_has_permissions ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ teams ) {
54
- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotPermission );
52
+ Schema::create ($ tableNames ['model_has_permissions ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ pivotPermission , $ teams ) {
53
+ $ table ->unsignedBigInteger ($ pivotPermission );
55
54
56
55
$ table ->string ('model_type ' );
57
56
$ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
58
57
$ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_permissions_model_id_model_type_index ' );
59
58
60
- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
59
+ $ table ->foreign ($ pivotPermission )
61
60
->references ('id ' ) // permission id
62
61
->on ($ tableNames ['permissions ' ])
63
62
->onDelete ('cascade ' );
64
63
if ($ teams ) {
65
64
$ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
66
65
$ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_permissions_team_foreign_key_index ' );
67
66
68
- $ table ->primary ([$ columnNames ['team_foreign_key ' ], PermissionRegistrar:: $ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
67
+ $ table ->primary ([$ columnNames ['team_foreign_key ' ], $ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
69
68
'model_has_permissions_permission_model_type_primary ' );
70
69
} else {
71
- $ table ->primary ([PermissionRegistrar:: $ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
70
+ $ table ->primary ([$ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
72
71
'model_has_permissions_permission_model_type_primary ' );
73
72
}
73
+
74
74
});
75
75
76
- Schema::create ($ tableNames ['model_has_roles ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ teams ) {
77
- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotRole );
76
+ Schema::create ($ tableNames ['model_has_roles ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ pivotRole , $ teams ) {
77
+ $ table ->unsignedBigInteger ($ pivotRole );
78
78
79
79
$ table ->string ('model_type ' );
80
80
$ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
81
81
$ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_roles_model_id_model_type_index ' );
82
82
83
- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
83
+ $ table ->foreign ($ pivotRole )
84
84
->references ('id ' ) // role id
85
85
->on ($ tableNames ['roles ' ])
86
86
->onDelete ('cascade ' );
87
87
if ($ teams ) {
88
88
$ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
89
89
$ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_roles_team_foreign_key_index ' );
90
90
91
- $ table ->primary ([$ columnNames ['team_foreign_key ' ], PermissionRegistrar:: $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
91
+ $ table ->primary ([$ columnNames ['team_foreign_key ' ], $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
92
92
'model_has_roles_role_model_type_primary ' );
93
93
} else {
94
- $ table ->primary ([PermissionRegistrar:: $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
94
+ $ table ->primary ([$ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
95
95
'model_has_roles_role_model_type_primary ' );
96
96
}
97
97
});
98
98
99
- Schema::create ($ tableNames ['role_has_permissions ' ], function (Blueprint $ table ) use ($ tableNames ) {
100
- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotPermission );
101
- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotRole );
99
+ Schema::create ($ tableNames ['role_has_permissions ' ], function (Blueprint $ table ) use ($ tableNames, $ pivotRole , $ pivotPermission ) {
100
+ $ table ->unsignedBigInteger ($ pivotPermission );
101
+ $ table ->unsignedBigInteger ($ pivotRole );
102
102
103
- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
103
+ $ table ->foreign ($ pivotPermission )
104
104
->references ('id ' ) // permission id
105
105
->on ($ tableNames ['permissions ' ])
106
106
->onDelete ('cascade ' );
107
107
108
- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
108
+ $ table ->foreign ($ pivotRole )
109
109
->references ('id ' ) // role id
110
110
->on ($ tableNames ['roles ' ])
111
111
->onDelete ('cascade ' );
112
112
113
- $ table ->primary ([PermissionRegistrar:: $ pivotPermission , PermissionRegistrar:: $ pivotRole ], 'role_has_permissions_permission_id_role_id_primary ' );
113
+ $ table ->primary ([$ pivotPermission , $ pivotRole ], 'role_has_permissions_permission_id_role_id_primary ' );
114
114
});
115
115
116
116
app ('cache ' )
@@ -120,10 +120,8 @@ public function up()
120
120
121
121
/**
122
122
* Reverse the migrations.
123
- *
124
- * @return void
125
123
*/
126
- public function down ()
124
+ public function down (): void
127
125
{
128
126
$ tableNames = config ('permission.table_names ' );
129
127
@@ -137,4 +135,4 @@ public function down()
137
135
Schema::drop ($ tableNames ['roles ' ]);
138
136
Schema::drop ($ tableNames ['permissions ' ]);
139
137
}
140
- }
138
+ };
0 commit comments