From 12fc3050450232432bea62a6c17eaeb8b48ef42e Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Sat, 13 Jun 2026 16:51:50 +0330 Subject: [PATCH] fix --- app/Traits/HasSaves.php | 52 ++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/app/Traits/HasSaves.php b/app/Traits/HasSaves.php index c5a5144..992e616 100644 --- a/app/Traits/HasSaves.php +++ b/app/Traits/HasSaves.php @@ -3,7 +3,6 @@ namespace App\Traits; use App\Models\SavedItem; -use Illuminate\Database\Eloquent\Relations\MorphMany; trait HasSaves { @@ -11,49 +10,58 @@ public function saves() { return $this->morphMany(SavedItem::class, 'saveable'); } - + public function getIsSavedAttribute() { - if (!auth()->check()) return false; - - return $this->saves() - ->where('user_id', auth()->id()) - ->exists(); + return $this->isSavedByAuthUser(); } - + public function getSavedCountAttribute() { return $this->saves()->count(); } - + + // Check the saves() relation directly (not the is_saved attribute, which a + // model may override to point at a different table — e.g. Media), so toggle + // always decides against the same table it writes to. + protected function isSavedByAuthUser(): bool + { + if (!auth()->check()) { + return false; + } + + return $this->saves()->where('user_id', auth()->id())->exists(); + } + public function toggleSaveStatus() { - if ($this->getIsSavedAttribute()) { - return $this->removeSave(); - } else { - return $this->addSave(); - } + return $this->isSavedByAuthUser() ? $this->removeSave() : $this->addSave(); } - + public function addSave() { - if ($this->getIsSavedAttribute()) return false; - - return SavedItem::create([ + if (!auth()->check()) { + return false; + } + + // Idempotent: never inserts a duplicate even on repeated/racing calls. + return SavedItem::firstOrCreate([ 'user_id' => auth()->id(), 'saveable_id' => $this->id, 'saveable_type' => get_class($this), ]); } - + public function removeSave() { - if (!$this->getIsSavedAttribute()) return false; - + if (!auth()->check()) { + return false; + } + return SavedItem::where([ 'user_id' => auth()->id(), 'saveable_id' => $this->id, 'saveable_type' => get_class($this), ])->delete(); } -} \ No newline at end of file +}