Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow readAt method to use in database channel #54729

Conversation

utsavsomaiya
Copy link
Contributor

Sometimes, you may want to create only the markedAsRead notification in the database driver.

To achieve this, follow these steps:

Before

  1. First way:
protected $listen = [
    \Illuminate\Notifications\Events\NotificationSent::class => [
        \App\Listeners\NotificationSentListener::class,
    ],
];
class NotificationSentListener
{
    public function handle(NotificationSent $event)
    {
        // Check if the notification was sent via the database channel
        if ($event->channel === 'database') {
            $notification = $event->notification;
            
            // The notification instance may not have an ID directly,
            // as the event is triggered after being sent to channels.
            // However, you can query the database using known data.
            
            // Mark the notification as read
        }
    }
}
  1. Second way:
  • Overwrite the buildPayload method, and whenever the service container requires DatabaseChannel, provide the overridden channel!

After

// In your notification
public function readAt(): Carbon
{
    Carbon::now();
}

@utsavsomaiya utsavsomaiya force-pushed the allow-mark-as-read-by-default-in-database-channel branch from 1476481 to e28b0ef Compare February 20, 2025 14:28
@utsavsomaiya utsavsomaiya changed the title [11.x] Notification: Add readAt method into database driver [11.x] Allow readAt method to use in database channel Feb 20, 2025
@taylorotwell taylorotwell merged commit 120f07f into laravel:11.x Feb 20, 2025
44 checks passed
@utsavsomaiya utsavsomaiya deleted the allow-mark-as-read-by-default-in-database-channel branch February 20, 2025 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants