-
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
Illuminate\Notifications\DatabaseNotification uses the $notifiable's database $connection #50564
Comments
Thanks. I see you attempted a PR but sent it to the wrong branch. Laravel v10 still receives bug fixes. Can you attempt your PR at 10.x? |
That is not my PR. And at a glance, it does not address or resolve what I've reported. It's just a differently thought work-around. Why close this issue without explanation or resolution? |
Isn't it the scenario that you're having notifications at primary connection and notifiable to another connection and want to have morph relation from different database? |
The fix is replacing the connection property for notifications |
Why does the framework's |
That's not a bug I guess. Notifiable trait accessing the instance of notifiable model and creates a morphMany relation with DatabaseNotification Model which is expected as there is no definition of where you have created the notifications table So, the PR offers an optional property to define which connection owns the notifications table. So, that you can work with several connections, even can create a separate connection only for the notifications table wihout having any problem |
If this were true, it follows that no cross-database relationships would work, in absence of an explicitly declared Is this the assertion? |
AssertI assert that framework models (e.g. And if there's to be a patch for overriding that – for example, for the inverse of the case I've described here – that it would best be designed as a configuration-based solution, such as publishing a |
irritating to have an issue summarily closed without being addressed or explained, @driesvints |
Calm down. Trying to read up 😅 I mistook you for being the same as @yourchocomate sorry. |
That's a good suggestion I guess. But I think notifications refers not only to the database channel. |
Left a comment on the PR. Will re-open this in the meantime. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Sorry, but right now it seems we're not undertaking any action here. |
The (faulty, I still assert) |
Worksas I'd expect, by blocking the implication that it should use the parent model's
// trait HasRelationships
protected function newRelatedInstance($class)
{
return tap(new $class, function ($instance) {
$instanceIsFramework = Str::contains(
(new \ReflectionClass($instance))->getNamespaceName(),
'Illuminate'
);
if (! $instance->getConnectionName() && ! $instanceIsFramework) {
$instance->setConnection($this->connection);
}
});
} Also worksas I'd expect, by just altogether short-circuiting the entire problematic assumption, that the child class ought to leech the parent's
// trait HasRelationships
protected function newRelatedInstance($class)
{
return new $class;
} This seems unnecessary. Notably, this |
Laravel Version
10.39
PHP Version
8.3
Database Driver & Version
MySQL 8.2.0
Description
The scenario is that the application leverages Laravel's and Eloquent's support for multiple databases, and by declaring the
$connection
for models which are in other than the primary laravel database (legacy databases, in this instance)..The problem arises when the Notifiable model (
Teacher
) is stored in a secondary or legacy database, denoted by theTeacher
model's$connection
property...Upon attempt at
$teacher->notify(new ReminderClassCheckin());
, I discovered that the database notification attempts to store itself using the notifiableTeacher
model's connection, instead of in the 'default' laravel database connection.Models in app/Models will use the primary database implicitly, without need of declaring a
$connection
property. Thus, that's the behavior I expected of this Framework model as well, but this turns out not to be the case.As is, to make this work the way I want and expect, it's necessary to override
\Illuminate\Notifications\{Notifiable, DatabaseNotifications, HasDatabaseNotifications}
... overriding or replacing the traits, and extending theDatabaseNotification
model class with the$connection
property.This all seems like it ought not be necessary. I'm a mere implementor, not a framework author, so it isn't immediately evident to me at which point in the lifecycle the database connection needs to be reset (or unset) to get the expected behavior; or, it's unclear to me why the behavior I'd expect is in fact, not supposed to be the expected behavior.
Steps To Reproduce
A laravel application with at least two database connections.
database
Behold:
The text was updated successfully, but these errors were encountered: