-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[6.x] Fix notifications database channel for anonymous notifiables #33409
Conversation
4223ea0
to
61fe94c
Compare
Hi, I ask you information only to clarify how works on-demand notification because from documentation it does not seem clear to me. Thank you very much! |
@illambo yes. I think the example in the docs makes this clear enough because it's listing multiple routes for different channels. If you feel anything should be clarified feel free to attempt a pr to the docs. |
Hi, yes the docs seems clear but in the issue #33408 when I set via method to mail and database and try with on-demand notification only for mail the notification was attempted to ship on both channels and not only for mail. |
@illambo that's exactly what this pr solves. |
Ok, sorry! |
@driesvints This is actually breaking our code
I now get the
What is the correct approach to also store a notification in the database for this? We use the database notifications to display all send notifications to a user in Nova. As I understand it, it's not possible to create a database notification anymore for anonymous users. However, this code doesn't create one for an anonymous user, right?
We are allowed to send in the query builder object that points to the current user. I think we need a fix for this. What do you think? |
Hey @bobbybouwmann. That's odd. I'm wondering why you're using it like that? Seems to me you need: $user->notifyNow(new CardActivationReminder($user, $card); The database channel for anonymous notifiables makes no sense since we can't store any record in the database (because we have no context of who the notifiable is (no morph column values). You posted: Notification::route('database', $user->notifications())
->notifyNow(new CardActivationReminder($user, $card); But that has no user context when sending the notification. So I'm wondering: does the |
Just to clarify the above. The problem was that a conditional check needed to be made in the public function via($notifiable): array
{
if ($notifiable->email) {
return ['email', 'database'];
}
return ['database'];
} |
Thanks @driesvints for debugging this together and come up with a solution. We will work on a refactor for it in our codebase 👍 |
This PR prevents on-demand notifications from using the database channel by throwing an informative exception when anyone tries it. At the moment you'd receive a fatal error when trying to resolve the routed notification.
It also will skip sending to the database channel of a notification if the notifiable is anonymous.
Fixes #33408