diff --git a/app/Http/Controllers/FavoriteController.php b/app/Http/Controllers/FavoriteController.php index 57dfc14..b8e030a 100644 --- a/app/Http/Controllers/FavoriteController.php +++ b/app/Http/Controllers/FavoriteController.php @@ -9,30 +9,24 @@ class FavoriteController extends Controller { public function index() { - $favorites=Favorite::with('tour') - ->where('user_id',auth()->id()) + $favorites = Favorite::with('favoritable') + ->where('user_id', auth()->id()) ->latest() ->get(); - return view('account.users.favorite.index',compact('favorites')); + + return view('account.users.favorite.index', compact('favorites')); } public function store(Request $request) { - $request->validate([ - 'tour_id'=>[ - 'required', - 'exists:tours,id' - ], - ]); Favorite::firstOrCreate([ 'user_id' => auth()->id(), - 'tour_id' => $request->tour_id, + 'favoritable_type' => $request->type, + 'favoritable_id' => $request->id, ]); - return back()->with( - 'success', - 'تور به علاقه‌مندی‌ها اضافه شد.' - ); + + return back(); } diff --git a/app/Models/Favorite.php b/app/Models/Favorite.php index c12b975..7582ec4 100644 --- a/app/Models/Favorite.php +++ b/app/Models/Favorite.php @@ -7,17 +7,17 @@ namespace App\Models; use Carbon\Carbon; -use Illuminate\Database\Eloquent\Model; + use Illuminate\Database\Eloquent\Model; /** * Class Favorite - * + * * @property int $id * @property int $user_id * @property string $favoritable_type * @property int $favoritable_id * @property Carbon|null $created_at - * + * * @property User $user * * @package App\Models @@ -43,4 +43,9 @@ class Favorite extends Model { return $this->belongsTo(User::class); } + public function favoritable() + { + return $this->morphTo(); + } + } diff --git a/app/Models/Flight.php b/app/Models/Flight.php index f695947..c026b5d 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -72,6 +72,10 @@ class Flight extends Model { return $this->hasMany(Comment::class); } + public function favorites() + { + return $this->morphMany(Favorite::class, 'favoritable'); + } #[Scope] public function applySearch(Builder $query, array $filters): void diff --git a/app/Models/Tour.php b/app/Models/Tour.php index e9cbe9e..aa5c8f0 100644 --- a/app/Models/Tour.php +++ b/app/Models/Tour.php @@ -62,4 +62,8 @@ class Tour extends Model { return $this->belongsTo(TravelType::class); } + public function favorites() + { + return $this->morphMany(Favorite::class, 'favoritable'); + } } diff --git a/resources/views/account/users/favorite/index.blade.php b/resources/views/account/users/favorite/index.blade.php index f7907f4..92a2608 100644 --- a/resources/views/account/users/favorite/index.blade.php +++ b/resources/views/account/users/favorite/index.blade.php @@ -93,74 +93,40 @@ @foreach($favorites as $favorite) -
+ @if($favorite->favoritable) -
+ @if($favorite->favoritable_type == 'App\Models\Tour') -
- - @if($favorite->tour->image) - {{ $favorite->tour->title }} - @else - {{ $favorite->tour->title }} - @endif - -
- - -
+

- {{ $favorite->tour->title }} + {{ $favorite->favoritable->title }}

-
- - -
- - - مشاهده تور - - - -
- @csrf - @method('DELETE') - - - -
+
-
-
+ @elseif($favorite->favoritable_type == 'App\Models\Flight') + +
+ +

+ {{ $favorite->favoritable->airline }} +

+ + + +
+ + @endif + + @endif @endforeach -
@else diff --git a/resources/views/components/tour.blade.php b/resources/views/components/tour.blade.php index 533b28d..5f09c06 100644 --- a/resources/views/components/tour.blade.php +++ b/resources/views/components/tour.blade.php @@ -1,7 +1,22 @@
+
+ @csrf + + + + + + + + +