From 7fd710bd9984ceb1014ea9f360158f781debe615 Mon Sep 17 00:00:00 2001 From: mina-esn Date: Tue, 18 Aug 2026 18:38:03 +0330 Subject: [PATCH] feat: implement favorites for tours and flights --- .../Account/FavoriteController.php | 84 ++++++++- .../Controllers/Account/ProfileController.php | 5 +- app/Models/Favorite.php | 15 +- .../views/account/favorite-list.blade.php | 159 ++++++++++++++++++ .../views/components/flight-card.blade.php | 20 ++- .../views/components/tour-card.blade.php | 63 ++++--- routes/web.php | 1 + 7 files changed, 311 insertions(+), 36 deletions(-) create mode 100644 resources/views/account/favorite-list.blade.php diff --git a/app/Http/Controllers/Account/FavoriteController.php b/app/Http/Controllers/Account/FavoriteController.php index a8d6550..35a99cf 100644 --- a/app/Http/Controllers/Account/FavoriteController.php +++ b/app/Http/Controllers/Account/FavoriteController.php @@ -3,33 +3,101 @@ namespace App\Http\Controllers\Account; use App\Http\Controllers\Controller; +use App\Models\Favorite; +use App\Models\Flight; +use App\Models\Tour; use Illuminate\Http\Request; class FavoriteController extends Controller { public function index() { - + $title = 'علاقه مندی های من'; + $favorites = Favorite::query() + ->where('user_id', auth()->id()) + ->with([ + 'tour.file', + 'tour.originCity', + 'tour.city', + 'tour.tourDepartures', + 'flight.airline', + 'flight.origin_airport.city', + 'flight.airport.city', + ]) + ->orderByDesc('created_at') + ->get(); + return view('account.favorite-list', compact('title','favorites')); } - public function toggleTour() + public function removeTour($tourId) + { + Favorite::where('user_id', auth()->id()) + ->where('tour_id', $tourId) + ->delete(); + return back()->with('success', 'با موفقیت حذف شد.'); + } + public function toggleTour(Tour $tour) { + if (!auth()->check()) { + return back()->with('error', 'برای افزودن به علاقهمندی ها ابتدا وارد شوید.'); + } + + $userId = auth()->id(); + + + $favorite = Favorite::where('user_id', $userId) + ->where('tour_id', $tour->id) + ->first(); + + if ($favorite) { + $favorite->delete(); + $message = 'از علاقهمندیها حذف شد.'; + } else { + Favorite::create([ + 'user_id' => $userId, + 'tour_id' => $tour->id, + ]); + $message = 'به علاقهمندیها اضافه شد.'; + } + + return back()->with('success', $message); } - public function removeTour() + public function removeFlight(Flight $flight) { + Favorite::where('user_id', auth()->id()) + ->where('flight_id', $flight->id) + ->delete(); + return back()->with('success', 'پرواز با موفقیت از علاقه‌مندی‌ها حذف شد.'); } - public function toggleFlight() + public function toggleFlight(Flight $flight) { + if (!auth()->check()) { + return back()->with('error', 'برای افزودن پرواز به علاقه‌مندی‌ها ابتدا وارد شوید.'); + } + $userId = auth()->id(); + + $favorite = Favorite::where('user_id', $userId) + ->where('flight_id', $flight->id) + ->first(); + + if ($favorite) { + $favorite->delete(); + $message = 'پرواز از علاقه‌مندی‌ها حذف شد.'; + } else { + Favorite::create([ + 'user_id' => $userId, + 'flight_id' => $flight->id, + ]); + $message = 'پرواز به لیست علاقه‌مندی‌ها اضافه شد.'; + } + + return back()->with('success', $message); } - public function removeFlight() - { - - } } diff --git a/app/Http/Controllers/Account/ProfileController.php b/app/Http/Controllers/Account/ProfileController.php index d043430..3ee4194 100644 --- a/app/Http/Controllers/Account/ProfileController.php +++ b/app/Http/Controllers/Account/ProfileController.php @@ -27,6 +27,7 @@ class ProfileController extends Controller public function comments() { + $title = 'کامنت های من'; $comments = auth()->user() ->comments() ->with([ @@ -36,8 +37,6 @@ class ProfileController extends Controller ->orderByDesc('created_at') ->paginate(10); - return view('account.comments' , compact('comments')); + return view('account.comments' , compact('title','comments')); } - - } diff --git a/app/Models/Favorite.php b/app/Models/Favorite.php index 4fef64f..c985299 100644 --- a/app/Models/Favorite.php +++ b/app/Models/Favorite.php @@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model; /** * Class Favorite - * + * * @property int $id * @property int $user_id * @property int|null $tour_id @@ -37,4 +37,17 @@ class Favorite extends Model 'tour_id', 'flight_id' ]; + + public function flight() + { + return $this->belongsTo(Flight::class, 'flight_id'); + } + public function tour() + { + return $this->belongsTo(Tour::class, 'tour_id'); + } + public function user() + { + return $this->belongsTo(User::class, 'user_id'); + } } diff --git a/resources/views/account/favorite-list.blade.php b/resources/views/account/favorite-list.blade.php new file mode 100644 index 0000000..a9fe42f --- /dev/null +++ b/resources/views/account/favorite-list.blade.php @@ -0,0 +1,159 @@ +@extends('account.layouts.app') + +@section('account-content') +
+ + @if (session('success')) +
+
+ {{ session('success') }} +
+
+ @endif + + @if (session('error')) +
+
+ {{ session('error') }} +
+
+ @endif + +
+ +
+ +
+@endsection diff --git a/resources/views/components/flight-card.blade.php b/resources/views/components/flight-card.blade.php index 83f296f..2317fe8 100644 --- a/resources/views/components/flight-card.blade.php +++ b/resources/views/components/flight-card.blade.php @@ -4,7 +4,25 @@
{{ $flight->flight_number }} - + @auth +
+ @csrf + + +
+ @else + + + + @endauth

diff --git a/resources/views/components/tour-card.blade.php b/resources/views/components/tour-card.blade.php index c3c698b..53b744d 100644 --- a/resources/views/components/tour-card.blade.php +++ b/resources/views/components/tour-card.blade.php @@ -1,30 +1,47 @@ +
+
+
+ {{ $tour->title }} + @auth +
+ @csrf -
-
-
- {{ $tour->title }} - -
-
-

- {{ $tour->title }} -

+ + + @else + + + + @endauth +
+
+

+ {{ $tour->title }} +

-

- {{ $tour->originCity?->name ?? 'مبدأ' }} - - {{ $tour->city?->name ?? 'مقصد' }} -

-
-
- {{ number_format(optional($tour->tourDepartures->first())->price ?? 0) }} تومان -
- +

+ {{ $tour->originCity->name ?? 'مبدأ' }} + + {{ $tour->city?->name ?? 'مقصد' }} +

+
+
+ {{ number_format(optional($tour->tourDepartures->first())->price ?? 0) }} تومان +
+
+
diff --git a/routes/web.php b/routes/web.php index 518efbd..c2c81b8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@