---
title: "Modely príjemcov a subjektov — Notifications for Laravel | Roundly"
description: "ReceivesNotifications, HasNotificationReminders a kontrakty, ktoré určia časové pásmo, členov skupiny, vlastníka alebo popis kontextu."
url: https://roundly-consulting.com/sk/open-source/navody/notifications-for-laravel/modely-prijemcov
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Notifications for Laravel](https://roundly-consulting.com/sk/open-source/navody/notifications-for-laravel.md)

# Modely príjemcov a subjektov

Dva traity a niekoľko malých kontraktov prepája vaše modely s balíkom. Každý je voliteľný — implementujte len to, čo vaše druhy používajú.

## ReceivesNotifications — príjemca

Použite ho na modeli User vedľa Notifiable z Laravelu. Každé volanie ide cez manažér, takže ho Notifications::fake() vidí:

```php
$user->inbox()->unread()->paginate();               // = Notifications::inbox($user)
$user->notificationPreferences()->mute('comment.posted');   // = Notifications::preferences($user)
$user->unreadNotificationCount();                   // 3
```

## HasNotificationReminders — subjekt

Použite ho na modeli, ktorého dátumy riadia pripomienky (zmluva, úloha, skúšobné obdobie). Pripomienky každého druhu, ktorého subject() je táto trieda — alebo jej rodič — sledujú jeho dátumové atribúty:

```php
use RoundlyConsulting\Notifications\Traits\HasNotificationReminders;

class Contract extends Model
{
    use HasNotificationReminders;
}

$contract->update(['expires_at' => now()->addYear()]);   // pending reminders move; sent ones re-arm
$contract->delete();                                      // pending reminders and escalations are cancelled
$contract->restore();                                     // and scheduled again
$contract->notificationReminders();                       // MorphMany<NotificationReminder>
$contract->syncNotificationReminders();                   // int — a manual sync
```

Synchronizuje sa pri created, pri updated, ak sa zmenil atribút kotvy zaregistrovaného pravidla (alebo čakajúcej jednorazovej pripomienky), a pri restored — až po vašom commite, takže čakanie na zámok ani prehratý súbeh nikdy nezhodí ani nevráti váš save() (chyby sa nahlásia). Zmazanie, soft aj natrvalo, ich zruší s dôvodom subject\_deleted. Trait je zároveň háčikom životného cyklu pre notifications.inbox.on\_subject\_deleted (ponechať, archivovať alebo zmazať položky schránky o subjekte).

## Kontrakty

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Notifications\Contracts\HasNotificationContext;
use RoundlyConsulting\Notifications\Contracts\HasNotificationOwner;
use RoundlyConsulting\Notifications\Contracts\HasNotificationTimezone;
use RoundlyConsulting\Notifications\Contracts\ProvidesNotificationRecipients;

// A recipient that names its own timezone — the first source the default resolver asks
class User extends Authenticatable implements HasNotificationTimezone
{
    use Notifiable;
    use ReceivesNotifications;

    public function notificationTimezone(): ?string
    {
        return $this->settings['timezone'] ?? null;
    }
}

// A group behind Recipients::role() / members(), and an inbox context with its own label
class Company extends Model implements ProvidesNotificationRecipients, HasNotificationContext
{
    public function notificationRecipients(?string $role = null): iterable
    {
        return $role === null
            ? $this->users
            : $this->users()->wherePivot('role', $role)->get();
    }

    public function notificationContextLabel(): string
    {
        return $this->legal_name;   // "Acme s.r.o."
    }
}

// A subject that names its owner, for Recipients::owner()
class Contract extends Model implements HasNotificationOwner
{
    use HasNotificationReminders;

    public function notificationOwner(): Model|iterable|null
    {
        return $this->accountManager;
    }
}
```

| Kontrakt | Metóda | Používa |
| --- | --- | --- |
| `HasNotificationTimezone` | `notificationTimezone(): ?string` | Predvolený resolver časového pásma — jeho prvý zdroj. |
| `ProvidesNotificationRecipients` | `notificationRecipients(?string $role = null): iterable` | Predvolený group resolver za Recipients::role() a members(). |
| `HasNotificationOwner` | `notificationOwner(): Model\|iterable\|null` | Recipients::owner(). |
| `HasNotificationContext` | `notificationContextLabel(): string` | Popis kontextu z modelu, inak jeho atribút name, inak názov triedy a kľúč. |
| `RecipientResolver` | `resolve(?Model $subject, array $args): iterable` | Recipients::using(). |
| `ResolvesGroupMembers` | `members(Model $group, ?string $role = null): iterable` | notifications.recipients.group\_resolver. |
| `ResolvesMailAddress` | `address(Model $recipient, ?NotificationPayload $payload): ?string` | notifications.mail.address\_resolver. |
| `ResolvesTimezone` | `timezone(Model $recipient): string` | notifications.timezone.resolver. |

## Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

[Ďalšie spôsoby podpory vrátane kryptomien](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/notifications-for-laravel.md)
