Mark as read once notification is opened

How can I mark a database notification as read once it is opened with the action I have configured?
protected function afterCreate(): void
{
$ticket = $this->record;
$recipient = User::query()->role('admin')->get();

Notification::make()
->title('New Ticket')
->icon('heroicon-o-ticket')
->body("**{$ticket->customer->name} has created a new ticket.**")
->actions([
Action::make('View')
->url(TicketResource::getUrl('edit', ['record' => $ticket])),
])
->sendToDatabase($recipient);
}
protected function afterCreate(): void
{
$ticket = $this->record;
$recipient = User::query()->role('admin')->get();

Notification::make()
->title('New Ticket')
->icon('heroicon-o-ticket')
->body("**{$ticket->customer->name} has created a new ticket.**")
->actions([
Action::make('View')
->url(TicketResource::getUrl('edit', ['record' => $ticket])),
])
->sendToDatabase($recipient);
}
And if the same notification is sent to different usersand if a user makes a change in a specific record field, is it possible to mark the rest of the notifications sent to the rest of the users as read?
3 Replies
Dan Harrin
Dan Harrin14mo ago
I added this feature to v3 yesterday its not been added to v2 yet
H.Bilbao
H.Bilbao14mo ago
perfect @Dan Harrin, looks good
undercode
undercode12mo ago
I solved it by updating 'read_at' field when the user enters in my ViewPage or EditPage.
public function mount($record): void
{
parent::mount($record);

$user = auth()->user();
$notification = DB::table('notifications')
->where('notifiable_type', get_class($user))
->where('notifiable_id', $user->id)
->where('data->actions[0]->url', 'like', '%issues/' . $this->record->id)
->whereNull('read_at');
$notification->update(['read_at' => now()]);
}
public function mount($record): void
{
parent::mount($record);

$user = auth()->user();
$notification = DB::table('notifications')
->where('notifiable_type', get_class($user))
->where('notifiable_id', $user->id)
->where('data->actions[0]->url', 'like', '%issues/' . $this->record->id)
->whereNull('read_at');
$notification->update(['read_at' => now()]);
}
You have to change '%issues/' with '%tickets/' or whatever your resource path is.