diff --git a/app/Http/Controllers/FavoriteController.php b/app/Http/Controllers/FavoriteController.php new file mode 100644 index 0000000..04d9d55 --- /dev/null +++ b/app/Http/Controllers/FavoriteController.php @@ -0,0 +1,74 @@ +user()->favorites()->with('favoritable')->get(); + + $items = $favorites->map(function ($favorite) { + $item = $favorite->favoritable; + + + + if ($item instanceof Flight) { + return [ + 'favorite_id' => $favorite->id, + 'type' => 'flight', + 'title' => 'پرواز ' . $item->flight_number, + 'subtitle' => ($item->originAirport->city ?? '') . ' به ' . ($item->destinationAirport->city ?? ''), + 'price' => number_format($item->price) . ' تومان', + 'favoritable_id' => $item->id, + 'favoritable_type' => 'flight', + ]; + } + + if ($item instanceof Tour) { + return [ + 'favorite_id' => $favorite->id, + 'type' => 'tour', + 'title' => $item->title, + 'subtitle' => $item->summary, + 'price' => null, + 'favoritable_id' => $item->id, + 'favoritable_type' => 'tour', + ]; + } + + return null; + })->filter()->values(); // حذف nullها و ریایندکس + + return view('favorites.index', compact('items' , 'favorites')); + } + public function toggle($type , $id) + { + + + $modelClass = $type === 'flight' ? Flight::class : Tour::class; + + $favorite = Favorite::where('user_id', auth()->id()) + ->where('favoritable_id', $id) + ->where('favoritable_type', $modelClass) + ->first(); + + if ($favorite) { + $favorite->delete(); + } else { + Favorite::create([ + 'user_id' => auth()->id(), + 'favoritable_id' => $id, + 'favoritable_type' => $modelClass, + ]); + } + + return back(); + } +} diff --git a/app/Models/Favorite.php b/app/Models/Favorite.php new file mode 100644 index 0000000..c557212 --- /dev/null +++ b/app/Models/Favorite.php @@ -0,0 +1,50 @@ + 'int', + 'favoritable_id' => 'int' + ]; + + protected $fillable = [ + 'user_id', + 'favoritable_id', + 'favoritable_type' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + public function favoritable() + { + return $this->morphTo(); + } +} diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 099a026..f19e3a8 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -98,4 +98,8 @@ class Flight extends Model { return $this->belongsTo(Airport::class, 'destination_airports_id'); } + public function favorites() + { + return $this->morphMany(Favorite::class, 'favoritable'); + } } diff --git a/app/Models/Tour.php b/app/Models/Tour.php index c846839..3512f37 100644 --- a/app/Models/Tour.php +++ b/app/Models/Tour.php @@ -74,4 +74,8 @@ class Tour extends Model { return $this->hasMany(TourDetail::class); } + public function favorites() + { + return $this->morphMany(Favorite::class, 'favoritable'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 51f4b78..e0ba453 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -72,4 +72,8 @@ class User extends Authenticated { return $this->belongsTo(File::class); } + public function favorites() + { + return $this->hasMany(Favorite::class); + } } diff --git a/resources/views/favorites/index.blade.php b/resources/views/favorites/index.blade.php new file mode 100644 index 0000000..5e66258 --- /dev/null +++ b/resources/views/favorites/index.blade.php @@ -0,0 +1,56 @@ +@extends('dashboard.layouts.app') + +@section('title' , 'علاقه مندی ها') + +@section('account-content') + +
+@endsection diff --git a/routes/web.php b/routes/web.php index 8bf0143..ee4ced5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ controller(TourController::class)->name('tour.')->group(f }); Route::prefix('flight')->controller(FlightController::class)->name('flight.')->group(function (){ Route::get('index' , 'index')->name('index'); - Route::get('show/{flight}' , 'show')->name('show'); +}); + +Route::middleware('auth')->group(function () { + Route::get('toggle/{type}/{id}', [FavoriteController::class, 'toggle'])->name('favorites.toggle'); + Route::get('/favorites', [FavoriteController::class, 'index'])->name('favorites.index'); });