11<?php
22
3- use Illuminate \Database \Migrations \Migration ;
4- use Illuminate \Database \Schema \Blueprint ;
53use Illuminate \Support \Facades \Schema ;
6- use Spatie \Permission \PermissionRegistrar ;
4+ use Illuminate \Database \Schema \Blueprint ;
5+ use Illuminate \Database \Migrations \Migration ;
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,67 +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 }
73+
7474 });
7575
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 );
7878
7979 $ table ->string ('model_type ' );
8080 $ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
8181 $ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_roles_model_id_model_type_index ' );
8282
83- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
83+ $ table ->foreign ($ pivotRole )
8484 ->references ('id ' ) // role id
8585 ->on ($ tableNames ['roles ' ])
8686 ->onDelete ('cascade ' );
8787 if ($ teams ) {
8888 $ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
8989 $ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_roles_team_foreign_key_index ' );
9090
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 ' ],
9292 'model_has_roles_role_model_type_primary ' );
9393 } else {
94- $ table ->primary ([PermissionRegistrar:: $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
94+ $ table ->primary ([$ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
9595 'model_has_roles_role_model_type_primary ' );
9696 }
9797 });
9898
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 );
102102
103- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
103+ $ table ->foreign ($ pivotPermission )
104104 ->references ('id ' ) // permission id
105105 ->on ($ tableNames ['permissions ' ])
106106 ->onDelete ('cascade ' );
107107
108- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
108+ $ table ->foreign ($ pivotRole )
109109 ->references ('id ' ) // role id
110110 ->on ($ tableNames ['roles ' ])
111111 ->onDelete ('cascade ' );
112112
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 ' );
114114 });
115115
116116 app ('cache ' )
@@ -120,10 +120,8 @@ public function up()
120120
121121 /**
122122 * Reverse the migrations.
123- *
124- * @return void
125123 */
126- public function down ()
124+ public function down (): void
127125 {
128126 $ tableNames = config ('permission.table_names ' );
129127
@@ -137,4 +135,4 @@ public function down()
137135 Schema::drop ($ tableNames ['roles ' ]);
138136 Schema::drop ($ tableNames ['permissions ' ]);
139137 }
140- }
138+ };
0 commit comments