Fenos\Notifynder\Notifications\NotificationRepository::deleteByCategory PHP Méthode

deleteByCategory() public méthode

Delete All notifications from a defined category.
public deleteByCategory ( $categoryName, $expired = false ) : boolean
$categoryName int
$expired Bool
Résultat boolean
    public function deleteByCategory($categoryName, $expired = false)
    {
        $query = $this->notification->whereHas('body', function ($query) use($categoryName) {
            $query->where('name', $categoryName);
        });
        if ($expired == true) {
            return $query->onlyExpired()->delete();
        }
        return $query->delete();
    }

Usage Example

 /** @test */
 function it_delete_all_notification_expired_by_category_name()
 {
     $category = $this->createCategory(['name' => 'test']);
     $this->createNotification(['category_id' => $category->id, 'expire_time' => Carbon\Carbon::now()->subDays(1)]);
     $this->createNotification(['category_id' => $category->id, 'expire_time' => Carbon\Carbon::now()->subDays(1)]);
     $this->createNotification(['category_id' => $category->id, 'expire_time' => Carbon\Carbon::now()->subDays(1)]);
     $this->createNotification(['category_id' => $category->id, 'expire_time' => Carbon\Carbon::now()->addDays(1)]);
     $this->notificationRepo->deleteByCategory($category->name, true);
     $this->assertCount(1, Notification::all());
 }