diff --git a/app/Models/PermissionRole.php b/app/Models/PermissionRole.php index 78fe298..7d7137d 100644 --- a/app/Models/PermissionRole.php +++ b/app/Models/PermissionRole.php @@ -3,9 +3,43 @@ namespace App\Models; use App\Models\Traits\LogsModel; -use Illuminate\Database\Eloquent\Relations\MorphPivot; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\Pivot; -class PermissionRole extends MorphPivot +class PermissionRole extends Pivot { use LogsModel; + + /** + * Get the table associated with the model. + * + * @return string + */ + public function getTable() + { + return config('permission.table_names.role_has_permissions', parent::getTable()); + } + + /** + * Indicates if the IDs are auto-incrementing. + * + * @var bool + */ + public $incrementing = true; + + /** + * @return BelongsTo + */ + public function role(): BelongsTo + { + return $this->belongsTo(Role::class); + } + + /** + * @return BelongsTo + */ + public function permission(): BelongsTo + { + return $this->belongsTo(Permission::class); + } } diff --git a/app/Models/RoleUser.php b/app/Models/RoleUser.php index 76c1ec0..91f91c4 100644 --- a/app/Models/RoleUser.php +++ b/app/Models/RoleUser.php @@ -3,9 +3,44 @@ namespace App\Models; use App\Models\Traits\LogsModel; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphPivot; +use Illuminate\Database\Eloquent\Relations\MorphTo; class RoleUser extends MorphPivot { use LogsModel; + + /** + * Get the table associated with the model. + * + * @return string + */ + public function getTable() + { + return config('permission.table_names.model_has_roles', parent::getTable()); + } + + /** + * Indicates if the IDs are auto-incrementing. + * + * @var bool + */ + public $incrementing = true; + + /** + * @return BelongsTo + */ + public function role(): BelongsTo + { + return $this->belongsTo(Role::class); + } + + /** + * @return MorphTo + */ + public function user(): MorphTo + { + return $this->morphTo('model'); + } } diff --git a/database/migrations/2024_09_13_032806_create_permission_tables.php b/database/migrations/2024_09_13_032806_create_permission_tables.php index 91fbf2e..16837f9 100644 --- a/database/migrations/2024_09_13_032806_create_permission_tables.php +++ b/database/migrations/2024_09_13_032806_create_permission_tables.php @@ -77,6 +77,7 @@ public function up(): void }); Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) { + $table->bigIncrements('id'); $table->unsignedBigInteger($pivotRole); $table->string('model_type'); @@ -94,12 +95,13 @@ public function up(): void $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'], 'model_has_roles_role_model_type_primary'); } else { - $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'], + $table->unique([$pivotRole, $columnNames['model_morph_key'], 'model_type'], 'model_has_roles_role_model_type_primary'); } }); Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) { + $table->bigIncrements('id'); $table->unsignedBigInteger($pivotPermission); $table->unsignedBigInteger($pivotRole); @@ -113,7 +115,7 @@ public function up(): void ->on($tableNames['roles']) ->onDelete('cascade'); - $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary'); + $table->unique([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary'); }); app('cache')