Skip to content

Commit

Permalink
Prevent EagerLoad when updating or counting notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdelrahman91 committed Jun 27, 2017
1 parent 349ed53 commit cd0d244
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Notifynder/Traits/Notifable.php
Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/Notifynder/Traits/NotifableBasic.php
Expand Up @@ -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.
Expand All @@ -35,7 +35,7 @@ public function getNotificationRelation($eagerLoad = null)
$with = $eagerLoad;
}

return $this->getLazyLoadedNotificationRelation()->with($with);
return $this->getLazyNotificationRelation()->with($with);
}

/**
Expand Down Expand Up @@ -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();
}

Expand All @@ -118,7 +118,7 @@ protected function updateSingleReadStatus($notification, $value)
*/
public function readAllNotifications()
{
return $this->getNotificationRelation()->update(['read' => 1]);
return $this->getLazyNotificationRelation()->update(['read' => 1]);
}

/**
Expand All @@ -128,7 +128,7 @@ public function readAllNotifications()
*/
public function unreadAllNotifications()
{
return $this->getNotificationRelation()->update(['read' => 0]);
return $this->getLazyNotificationRelation()->update(['read' => 0]);
}

/**
Expand All @@ -138,7 +138,7 @@ public function unreadAllNotifications()
*/
public function countUnreadNotifications()
{
return $this->getNotificationRelation()->byRead(0)->count();
return $this->getLazyNotificationRelation()->byRead(0)->count();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Notifynder/Traits/NotifableLaravel53.php
Expand Up @@ -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();
}
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/Traits/NotifableEagerLoadingTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit cd0d244

Please sign in to comment.