Fenos\Notifynder\Notifications\NotificationRepository::getLastNotificationByCategory PHP Метод

getLastNotificationByCategory() публичный Метод

Get last notification of the current entity of a specific category.
public getLastNotificationByCategory ( $category, $toId, $entity, Closur\Closure $filterScope = null ) : mixed
$category
$toId
$entity
$filterScope Closur\Closure
Результат mixed
    public function getLastNotificationByCategory($category, $toId, $entity, Closure $filterScope = null)
    {
        $query = $this->notification->wherePolymorphic($toId, $entity)->byCategory($category)->orderBy('created_at', 'desc');
        $query = $this->applyFilter($filterScope, $query);
        return $query->first();
    }

Usage Example

 /** @test */
 function it_get_the_last_notificiation_sent_by_category()
 {
     $category1 = $this->createCategory(['name' => 'test']);
     $category2 = $this->createCategory(['name' => 'test2']);
     $this->createNotification(['category_id' => $category1->id, 'url' => 'first', 'to_id' => 1, 'created_at' => Carbon\Carbon::now()->addDay(1)]);
     $this->createNotification(['category_id' => $category1->id, 'url' => 'second', 'to_id' => 1, 'created_at' => Carbon\Carbon::now()->addDay(2)]);
     $this->createNotification(['category_id' => $category2->id, 'url' => 'third', 'to_id' => 1, 'created_at' => Carbon\Carbon::now()->addDay(3)]);
     $notificationByName = $this->notificationRepo->getLastNotificationByCategory('test', 1, null);
     $notificationById = $this->notificationRepo->getLastNotificationByCategory($category1->id, 1, null);
     $this->assertEquals('second', $notificationByName->url);
     $this->assertEquals('second', $notificationById->url);
 }