| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace App\Observers;
- use App\Broadcasting\RealtimeEvent;
- use App\Broadcasting\RealtimeService;
- use App\Models\Notification;
- class NotificationObserver
- {
- public function __construct(
- private RealtimeService $realtime
- ) {}
- public function created(Notification $notification): void
- {
- $this->realtime->emitToUser(
- RealtimeEvent::NOTIFICATION_CREATED,
- $notification->user_id,
- [
- 'entity' => 'notification',
- 'id' => $notification->id,
- 'type' => $notification->type,
- ],
- );
- }
- public function updated(Notification $notification): void
- {
- if (! $notification->wasChanged('read') || ! $notification->read) {
- return;
- }
- $this->realtime->emitToUser(
- RealtimeEvent::NOTIFICATION_READ,
- $notification->user_id,
- [
- 'entity' => 'notification',
- 'id' => $notification->id,
- ],
- );
- }
- }
|