Skip to content

Commit 487097b

Browse files
committed
Explicitly delete replies
1 parent 67ba205 commit 487097b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

app/Helpers/ReceivesReplies.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Helpers;
44

5+
use App\Models\Like;
56
use App\Models\Reply;
67
use Illuminate\Database\Eloquent\Relations\MorphMany;
78

@@ -25,7 +26,11 @@ public function latestReplies(int $amount = 5)
2526

2627
public function deleteReplies()
2728
{
28-
$this->repliesRelation()->delete();
29+
// We need to explicitly iterate over the repies and delete them
30+
// separately because all related models need to be deleted.
31+
foreach ($this->repliesRelation()->get() as $reply) {
32+
$reply->delete();
33+
}
2934
}
3035

3136
/**

app/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ public function latestReplies(int $amount = 3)
189189

190190
public function deleteReplies()
191191
{
192-
$this->replyAble()->delete();
192+
// We need to explicitly iterate over the replies and delete them
193+
// separately because all related models need to be deleted.
194+
foreach ($this->replyAble()->get() as $reply) {
195+
$reply->delete();
196+
}
193197
}
194198

195199
public function countReplies(): int

0 commit comments

Comments
 (0)