-
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
[11.x] Added notifiable connection property to specify database connection for notifications #50570
Conversation
Please send this to 10.x |
Thought, it could be a feature for 11.x Am, sending this to 10.x |
Should I make any modifications or it's okay to be opened in 10.x Suggestions are appreciated! |
Sorry I thought this was a bug fix. Happy to re-open this for 11.x |
Thank you, am not making PR for 10.x then. Or, Should it be available for both 10.x and 11.x? |
@yourchocomate You should fix the 'StyleCI' issue in your code: https://github.styleci.io/analyses/MPRQ7v |
Thanks, I'm updating it. |
@@ -11,7 +11,9 @@ trait HasDatabaseNotifications | |||
*/ | |||
public function notifications() | |||
{ | |||
return $this->morphMany(DatabaseNotification::class, 'notifiable')->latest(); | |||
return $this->setConnection($this->getNotifiableConnectionName()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reviewing this, I'm not sure this is the right approach as this will set the connection on the model class that has HasDatabaseNotifications
applied. What you want to do instead is set the connection on the retrieved DatabaseNotification
instances. But that can't be done here.
This is a tough nut to crack. I think the only way to do this is to introduce a new config setting somewhere where the default database connection of the |
Should I work on it? I will convert this PR to draft then. |
@yourchocomate want to see what @taylorotwell has to say first. |
Is it understood why it doesn't default to EDIT if I just add this to public function __construct()
{
$this->connection = config('database.notifications') ?: config('database.default');
} |
Thought of that fix but made my mind think if this approach is not standard approach of explicitly accessing config like that. Waiting for the suggestion for right approach from @taylorotwell |
Atm I do not immediately see how
You shouldn't overwrite a model's constructor as that's not supported and can lead to a variety of bugs. |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
Description:
This pull request addresses the issue where Laravel's notification system does not behave as expected when dealing with models stored in secondary or legacy databases (introduced at issue #50564). Currently, notifications attempt to store themselves using the database connection of the notifiable model, even if it's not the primary Laravel connection.
To solve this problem, this pull request introduces a new
$notifiableConnection
property to the Illuminate\Notifications\Notifiable trait. This property allows developers to specify the database connection to be used for notifications, overriding the connection specified in the notifiable model as new feature for 11.xUsage:
Testing:
Any feedback or suggestions for improvement are welcome.