diff --git a/app/Http/Controllers/FavoriteController.php b/app/Http/Controllers/FavoriteController.php new file mode 100644 index 0000000..8895495 --- /dev/null +++ b/app/Http/Controllers/FavoriteController.php @@ -0,0 +1,43 @@ +input('favoritable_type'); + $id = $request->input('favoritable_id'); + + // چک کنید اگر null بود، متوقف شود + if (!$type || !$id) { + return back()->withErrors('اطلاعات ارسالی ناقص است!'); + } + + $user = auth()->user(); + + + $exists = $user->favorites() + ->where('favoritable_type', $type) + ->where('favoritable_id', $id) + ->first(); + + if ($exists) { + // اگر وجود داشت، حذفش کن + $exists->delete(); + $message = 'از علاقه‌مندی‌ها حذف شد.'; + } else { + // اگر وجود نداشت، یک رکورد جدید بساز + $user->favorites()->create([ + 'favoritable_type' => $type, + 'favoritable_id' => $id, + ]); + $message = 'به علاقه‌مندی‌ها اضافه شد.'; + } + + return back()->with('success', $message); + } +}