33use Illuminate \Support \Facades \Schema ;
44use Illuminate \Database \Schema \Blueprint ;
55use Illuminate \Database \Migrations \Migration ;
6- use Spatie \Permission \PermissionRegistrar ;
76
8- class CreatePermissionTables extends Migration
7+ return new class extends Migration
98{
109 /**
1110 * Run the migrations.
12- *
13- * @return void
1411 */
15- public function up ()
12+ public function up (): void
1613 {
14+ $ teams = config ('permission.teams ' );
1715 $ tableNames = config ('permission.table_names ' );
1816 $ 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 ' ;
2019
2120 if (empty ($ tableNames )) {
2221 throw new \Exception ('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again. ' );
@@ -50,68 +49,68 @@ public function up()
5049 }
5150 });
5251
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 );
5554
5655 $ table ->string ('model_type ' );
5756 $ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
5857 $ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_permissions_model_id_model_type_index ' );
5958
60- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
59+ $ table ->foreign ($ pivotPermission )
6160 ->references ('id ' ) // permission id
6261 ->on ($ tableNames ['permissions ' ])
6362 ->onDelete ('cascade ' );
6463 if ($ teams ) {
6564 $ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
6665 $ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_permissions_team_foreign_key_index ' );
6766
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 ' ],
6968 'model_has_permissions_permission_model_type_primary ' );
7069 } else {
71- $ table ->primary ([PermissionRegistrar:: $ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
70+ $ table ->primary ([$ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
7271 'model_has_permissions_permission_model_type_primary ' );
7372 }
7473
7574 });
7675
77- Schema::create ($ tableNames ['model_has_roles ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ teams ) {
78- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotRole );
76+ Schema::create ($ tableNames ['model_has_roles ' ], function (Blueprint $ table ) use ($ tableNames , $ columnNames , $ pivotRole , $ teams ) {
77+ $ table ->unsignedBigInteger ($ pivotRole );
7978
8079 $ table ->string ('model_type ' );
8180 $ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
8281 $ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_roles_model_id_model_type_index ' );
8382
84- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
83+ $ table ->foreign ($ pivotRole )
8584 ->references ('id ' ) // role id
8685 ->on ($ tableNames ['roles ' ])
8786 ->onDelete ('cascade ' );
8887 if ($ teams ) {
8988 $ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
9089 $ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_roles_team_foreign_key_index ' );
9190
92- $ 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 ' ],
9392 'model_has_roles_role_model_type_primary ' );
9493 } else {
95- $ table ->primary ([PermissionRegistrar:: $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
94+ $ table ->primary ([$ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
9695 'model_has_roles_role_model_type_primary ' );
9796 }
9897 });
9998
100- Schema::create ($ tableNames ['role_has_permissions ' ], function (Blueprint $ table ) use ($ tableNames ) {
101- $ table ->unsignedBigInteger (PermissionRegistrar:: $ pivotPermission );
102- $ 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 );
103102
104- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
103+ $ table ->foreign ($ pivotPermission )
105104 ->references ('id ' ) // permission id
106105 ->on ($ tableNames ['permissions ' ])
107106 ->onDelete ('cascade ' );
108107
109- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
108+ $ table ->foreign ($ pivotRole )
110109 ->references ('id ' ) // role id
111110 ->on ($ tableNames ['roles ' ])
112111 ->onDelete ('cascade ' );
113112
114- $ 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 ' );
115114 });
116115
117116 app ('cache ' )
@@ -121,10 +120,8 @@ public function up()
121120
122121 /**
123122 * Reverse the migrations.
124- *
125- * @return void
126123 */
127- public function down ()
124+ public function down (): void
128125 {
129126 $ tableNames = config ('permission.table_names ' );
130127
@@ -138,4 +135,4 @@ public function down()
138135 Schema::drop ($ tableNames ['roles ' ]);
139136 Schema::drop ($ tableNames ['permissions ' ]);
140137 }
141- }
138+ };
0 commit comments