diff --git a/src/Notifynder/Traits/Notifable.php b/src/Notifynder/Traits/Notifable.php index e901ace..469dd9e 100644 --- a/src/Notifynder/Traits/Notifable.php +++ b/src/Notifynder/Traits/Notifable.php @@ -53,7 +53,7 @@ public function notifications() * * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany */ - protected function getLazyLoadedNotificationRelation() + public function getLazyNotificationRelation() { return $this->notifications(); } diff --git a/src/Notifynder/Traits/NotifableBasic.php b/src/Notifynder/Traits/NotifableBasic.php index 6bc4ee5..4e32931 100644 --- a/src/Notifynder/Traits/NotifableBasic.php +++ b/src/Notifynder/Traits/NotifableBasic.php @@ -14,7 +14,7 @@ trait NotifableBasic * * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany */ - abstract protected function getLazyLoadedNotificationRelation(); + abstract public function getLazyNotificationRelation(); /** * Get the notifications Relationship. @@ -35,7 +35,7 @@ public function getNotificationRelation($eagerLoad = null) $with = $eagerLoad; } - return $this->getLazyLoadedNotificationRelation()->with($with); + return $this->getLazyNotificationRelation()->with($with); } /** @@ -101,10 +101,10 @@ public function unreadNotification($notification) protected function updateSingleReadStatus($notification, $value) { if (! TypeChecker::isNotification($notification, false)) { - $notification = $this->getNotificationRelation()->findOrFail($notification); + $notification = $this->getLazyNotificationRelation()->findOrFail($notification); } - if ($this->getNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) { + if ($this->getLazyNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) { return $value ? $notification->read() : $notification->unread(); } @@ -118,7 +118,7 @@ protected function updateSingleReadStatus($notification, $value) */ public function readAllNotifications() { - return $this->getNotificationRelation()->update(['read' => 1]); + return $this->getLazyNotificationRelation()->update(['read' => 1]); } /** @@ -128,7 +128,7 @@ public function readAllNotifications() */ public function unreadAllNotifications() { - return $this->getNotificationRelation()->update(['read' => 0]); + return $this->getLazyNotificationRelation()->update(['read' => 0]); } /** @@ -138,7 +138,7 @@ public function unreadAllNotifications() */ public function countUnreadNotifications() { - return $this->getNotificationRelation()->byRead(0)->count(); + return $this->getLazyNotificationRelation()->byRead(0)->count(); } /** diff --git a/src/Notifynder/Traits/NotifableLaravel53.php b/src/Notifynder/Traits/NotifableLaravel53.php index 0974c55..8efec2f 100644 --- a/src/Notifynder/Traits/NotifableLaravel53.php +++ b/src/Notifynder/Traits/NotifableLaravel53.php @@ -53,7 +53,7 @@ public function notifynderNotifications() * * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany */ - protected function getLazyLoadedNotificationRelation() + public function getLazyNotificationRelation() { return $this->notifynderNotifications(); } diff --git a/tests/integration/Traits/NotifableEagerLoadingTest.php b/tests/integration/Traits/NotifableEagerLoadingTest.php index 3dcff67..9140098 100644 --- a/tests/integration/Traits/NotifableEagerLoadingTest.php +++ b/tests/integration/Traits/NotifableEagerLoadingTest.php @@ -2,6 +2,15 @@ class NotifableEagerLoadingTest extends NotifynderTestCase { + public function testGetLazyNotificationRelationDoesnotEagerLoad() + { + $user = $this->createUser(); + $this->sendNotificationTo($user); + + $notification = $user->getLazyNotificationRelation()->first(); + $this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); + } + public function testGetNotificationRelationReadsConfigurationParameterIfNothingIsPassed() { $user = $this->createUser();