NovinkaZverejnili sme 50+ Laravel balíkov ako open source
Custom AI apps, agents and automation — Roundly ConsultingRoundly
Všetky balíky
Notifications for Laravel

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();                   // 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:

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

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;
    }
}
KontraktMetódaPoužíva
HasNotificationTimezonenotificationTimezone(): ?stringPredvolený resolver časového pásma — jeho prvý zdroj.
ProvidesNotificationRecipientsnotificationRecipients(?string $role = null): iterablePredvolený group resolver za Recipients::role() a members().
HasNotificationOwnernotificationOwner(): Model|iterable|nullRecipients::owner().
HasNotificationContextnotificationContextLabel(): stringPopis kontextu z modelu, inak jeho atribút name, inak názov triedy a kľúč.
RecipientResolverresolve(?Model $subject, array $args): iterableRecipients::using().
ResolvesGroupMembersmembers(Model $group, ?string $role = null): iterablenotifications.recipients.group_resolver.
ResolvesMailAddressaddress(Model $recipient, ?NotificationPayload $payload): ?stringnotifications.mail.address_resolver.
ResolvesTimezonetimezone(Model $recipient): stringnotifications.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

Odoslaní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.