From 33390125aea5098b219525314b0fb3b0aaa2ae1e Mon Sep 17 00:00:00 2001 From: "Victor.Sergeyev" Date: Fri, 4 Feb 2022 14:16:32 +0400 Subject: [PATCH 1/3] fixed: roles n+1 queries --- src/Traits/HasRoleAndPermission.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Traits/HasRoleAndPermission.php b/src/Traits/HasRoleAndPermission.php index ec16394..04e245a 100755 --- a/src/Traits/HasRoleAndPermission.php +++ b/src/Traits/HasRoleAndPermission.php @@ -44,7 +44,11 @@ public function roles() */ public function getRoles() { - return (!$this->roles) ? $this->roles = $this->roles()->get() : $this->roles; + if (!$this->roles) { + $this->loadMissing('roles'); + $this->roles = $this->getRelation('roles'); + } + return $this->roles; } /** @@ -131,6 +135,7 @@ public function attachRole($role) return true; } $this->roles = null; + $this->unsetRelation('roles'); return $this->roles()->attach($role); } @@ -145,6 +150,7 @@ public function attachRole($role) public function detachRole($role) { $this->roles = null; + $this->unsetRelation('roles'); return $this->roles()->detach($role); } @@ -157,6 +163,7 @@ public function detachRole($role) public function detachAllRoles() { $this->roles = null; + $this->unsetRelation('roles'); return $this->roles()->detach(); } @@ -171,6 +178,7 @@ public function detachAllRoles() public function syncRoles($roles) { $this->roles = null; + $this->unsetRelation('roles'); return $this->roles()->sync($roles); } From 0c841125dd01494a1dd927ef94b571680646bdcf Mon Sep 17 00:00:00 2001 From: Victor <32728904+ulcuber@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:26:40 +0400 Subject: [PATCH 2/3] fixed: style issues --- src/Traits/HasRoleAndPermission.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Traits/HasRoleAndPermission.php b/src/Traits/HasRoleAndPermission.php index 04e245a..cc94076 100755 --- a/src/Traits/HasRoleAndPermission.php +++ b/src/Traits/HasRoleAndPermission.php @@ -48,6 +48,7 @@ public function getRoles() $this->loadMissing('roles'); $this->roles = $this->getRelation('roles'); } + return $this->roles; } From 872755fa780b3ae6062be06fed0291de4e626af3 Mon Sep 17 00:00:00 2001 From: "Victor.Sergeyev" Date: Fri, 4 Feb 2022 16:41:45 +0400 Subject: [PATCH 3/3] fixed: old laravel compatibility --- src/Traits/HasRoleAndPermission.php | 36 +++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Traits/HasRoleAndPermission.php b/src/Traits/HasRoleAndPermission.php index cc94076..ff009b2 100755 --- a/src/Traits/HasRoleAndPermission.php +++ b/src/Traits/HasRoleAndPermission.php @@ -45,8 +45,18 @@ public function roles() public function getRoles() { if (!$this->roles) { - $this->loadMissing('roles'); - $this->roles = $this->getRelation('roles'); + if (method_exists($this, 'loadMissing')) { + $this->loadMissing('roles'); + } else { + if (!array_key_exists('roles', $this->relations)) { + $this->load('roles'); + } + } + if (method_exists($this, 'getRelation')) { + $this->roles = $this->getRelation('roles'); + } else { + $this->roles = $this->relations['roles']; + } } return $this->roles; @@ -135,8 +145,7 @@ public function attachRole($role) if ($this->getRoles()->contains($role)) { return true; } - $this->roles = null; - $this->unsetRelation('roles'); + $this->resetRoles(); return $this->roles()->attach($role); } @@ -150,8 +159,7 @@ public function attachRole($role) */ public function detachRole($role) { - $this->roles = null; - $this->unsetRelation('roles'); + $this->resetRoles(); return $this->roles()->detach($role); } @@ -163,8 +171,7 @@ public function detachRole($role) */ public function detachAllRoles() { - $this->roles = null; - $this->unsetRelation('roles'); + $this->resetRoles(); return $this->roles()->detach(); } @@ -178,8 +185,7 @@ public function detachAllRoles() */ public function syncRoles($roles) { - $this->roles = null; - $this->unsetRelation('roles'); + $this->resetRoles(); return $this->roles()->sync($roles); } @@ -451,6 +457,16 @@ private function getArrayFrom($argument) return (!is_array($argument)) ? preg_split('/ ?[,|] ?/', $argument) : $argument; } + protected function resetRoles() + { + $this->roles = null; + if (method_exists($this, 'unsetRelation')) { + $this->unsetRelation('roles'); + } else { + unset($this->relations['roles']); + } + } + public function callMagic($method, $parameters) { if (starts_with($method, 'is')) {