Skip to content

Commit 9de2943

Browse files
committed
Notifications: Fixed error on comment notification
Fixes an error where a used relation (entity) on the comment was resulting in null due to eager loading the notification when deserializing from the queue, where Laravel was then mis-matching the names when performing the eager loading. For #5918
1 parent 98a09bc commit 9de2943

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

app/Activity/Models/Comment.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ class Comment extends Model implements Loggable, OwnableInterface
4141
*/
4242
public function entity(): MorphTo
4343
{
44-
return $this->morphTo('commentable');
44+
// We specifically define null here to avoid the different name (commentable)
45+
// being used by Laravel eager loading instead of the method name, which it was doing
46+
// in some scenarios like when deserialized when going through the queue system.
47+
// So we instead specify the type and id column names to use.
48+
// Related to:
49+
// https://github.com/laravel/framework/pull/24815
50+
// https://github.com/laravel/framework/issues/27342
51+
// https://github.com/laravel/framework/issues/47953
52+
// (and probably more)
53+
54+
// Ultimately, we could just align the method name to 'commentable' but that would be a potential
55+
// breaking change and not really worthwhile in a patch due to the risk of creating extra problems.
56+
return $this->morphTo(null, 'commentable_type', 'commentable_id');
4557
}
4658

4759
/**

app/Activity/Notifications/Handlers/BaseNotificationHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected function sendNotificationToUserIds(string $notification, array $userId
2020
{
2121
$users = User::query()->whereIn('id', array_unique($userIds))->get();
2222

23+
/** @var User $user */
2324
foreach ($users as $user) {
2425
// Prevent sending to the user that initiated the activity
2526
if ($user->id === $initiator->id) {

0 commit comments

Comments
 (0)