Skip to content

Commit 2c9391f

Browse files
committed
fix
1 parent f08a7b4 commit 2c9391f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Message.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function markAsReadBy(User|int $user, ?Carbon $date = null): static
139139
{
140140
$userId = $user instanceof User ? $user->getKey() : $user;
141141

142-
$read = $this->reads()->firstOrNew([
142+
$read = static::getModelRead()::query()->firstOrNew([
143143
'user_id' => $userId,
144144
'message_id' => $this->id,
145145
]);
@@ -161,11 +161,19 @@ public function markAsUnreadBy(User|int $user): static
161161
{
162162
$userId = $user instanceof User ? $user->getKey() : $user;
163163

164-
$read = $this->reads->firstWhere('user_id', $userId);
164+
$read = static::getModelRead()::query()->firstOrNew([
165+
'user_id' => $userId,
166+
'message_id' => $this->id,
167+
]);
168+
169+
$read->read_at = null;
170+
$read->save();
165171

166-
if ($read) {
167-
$read->read_at = null;
168-
$read->save();
172+
if ($this->relationLoaded('reads')) {
173+
$this->setRelation(
174+
'reads',
175+
$this->reads->except([$read])->push($read)
176+
);
169177
}
170178

171179
return $this;

0 commit comments

Comments
 (0)