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') +
+@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 @@
+ @auth
+