feat: implement user favorites and fix toggle functionality

This commit is contained in:
2026-08-22 08:34:29 +03:30
parent b0bf4838ba
commit 44b18e4957
6 changed files with 180 additions and 110 deletions
+12 -11
View File
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Enums\CommentStatus;
use App\Http\Requests\Tour\TourCommentStoreRequest;
use App\Models\Comment;
use App\Models\Favorite;
use App\Models\Tour;
use Illuminate\Http\Request;
@@ -30,7 +31,7 @@ class TourController extends Controller
public function show(Tour $tour)
{
$title= 'جزئیات تور';
$title = 'جزئیات تور';
$tour->load([
'file',
'originCity',
@@ -46,26 +47,26 @@ class TourController extends Controller
$isFavorite = false;
if (auth()->check()) {
$isFavorite = auth()->user()
->favoriteTours()
->where('tours.id', $tour->id)
$isFavorite = Favorite::where('user_id', auth()->id())
->where('tour_id', $tour->id)
->exists();
}
return view('tours.show', compact('title' , 'tour'));
return view('tours.show', compact('title', 'tour', 'isFavorite'));
}
public function commentStore(TourCommentStoreRequest $request, Tour $tour)
{
try {
Comment::create([
'tour_id' => $tour->id,
'user_id' => auth()->id(),
'parent_id' => $request->parent_id,
'guest_name' => auth()->check() ? null : $request->guest_name,
'tour_id' => $tour->id,
'user_id' => auth()->id(),
'parent_id' => $request->parent_id,
'guest_name' => auth()->check() ? null : $request->guest_name,
'guest_email' => auth()->check() ? null : $request->guest_email,
'content' => $request->content,
'status' => CommentStatus::PENDING,
'content' => $request->content,
'status' => CommentStatus::PENDING,
]);
return back()->with('success', 'نظر شما با موفقیت ثبت شد.');