From d863ce82a48d55c5b96719fb3124050297593a18 Mon Sep 17 00:00:00 2001 From: subho biswas Date: Mon, 7 Oct 2024 19:19:08 +0530 Subject: [PATCH] update the functionality --- src/Models/Permission.php | 6 +++--- src/Models/Role.php | 4 ++-- ...issions.php => HasRolesAndPermissions.php} | 0 .../migrations/create_permission_tables.php | 19 ++++++++++++------- 4 files changed, 17 insertions(+), 12 deletions(-) rename src/Traits/{HasRoleAndPermissions.php => HasRolesAndPermissions.php} (100%) diff --git a/src/Models/Permission.php b/src/Models/Permission.php index d275ac0..2ce3f63 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -1,15 +1,15 @@ belongsToMany(Role::class); + return $this->belongsToMany(Role::class, 'role_permission'); } } diff --git a/src/Models/Role.php b/src/Models/Role.php index 68075e0..faee67e 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -1,6 +1,6 @@ belongsToMany(Permission::class); + return $this->belongsToMany(Permission::class, 'role_permission'); } } diff --git a/src/Traits/HasRoleAndPermissions.php b/src/Traits/HasRolesAndPermissions.php similarity index 100% rename from src/Traits/HasRoleAndPermissions.php rename to src/Traits/HasRolesAndPermissions.php diff --git a/src/database/migrations/create_permission_tables.php b/src/database/migrations/create_permission_tables.php index 317f16a..4b82a00 100644 --- a/src/database/migrations/create_permission_tables.php +++ b/src/database/migrations/create_permission_tables.php @@ -32,10 +32,12 @@ public function up() }); } - if (!Schema::hasTable('users')) { - Schema::table('users', function (Blueprint $table) { - $table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null'); - }); + if (Schema::hasTable('users')) { + if (!Schema::hasColumn('users', 'role_id')) { + Schema::table('users', function (Blueprint $table) { + $table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null'); + }); + } } } @@ -45,8 +47,11 @@ public function down() Schema::dropIfExists('role_permission'); Schema::dropIfExists('permissions'); Schema::dropIfExists('roles'); - Schema::table('users', function (Blueprint $table) { - $table->dropForeign(['role_id']); - }); + if (Schema::hasTable('users') && Schema::hasColumn('users', 'role_id')) { + Schema::table('users', function (Blueprint $table) { + $table->dropForeign(['role_id']); + $table->dropColumn('role_id'); + }); + } } };