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í:
$user->inbox()->unread()->paginate(); // = Notifications::inbox($user)
$user->notificationPreferences()->mute('comment.posted'); // = Notifications::preferences($user)
$user->unreadNotificationCount(); // 3HasNotificationReminders — 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:
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 syncSynchronizuje 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
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 kryptomienOdoslaním daru súhlasíte s našimi podmienkami prijímania darov.
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.