From f137797e3bd195314a8b6d0d60784b281cc1775a Mon Sep 17 00:00:00 2001 From: mina-esn Date: Mon, 17 Aug 2026 09:59:20 +0330 Subject: [PATCH] feat: update models and relationships --- app/Models/City.php | 17 +++++++++++------ app/Models/Flight.php | 24 ++++++++++++++++++++++++ app/Models/Tour.php | 15 ++++++++++++--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/app/Models/City.php b/app/Models/City.php index c204b7c..dd71423 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; /** * Class City - * + * * @property int $id * @property int $county_id * @property int $province_id @@ -22,7 +22,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property Carbon $created_at * @property Carbon $updated_at * @property string|null $deleted_at - * + * * @property Country $country * @property Province $province * @property Collection|Airport[] $airports @@ -65,10 +65,15 @@ class City extends Model return $this->hasMany(Airport::class); } - public function tours() - { - return $this->hasMany(Tour::class, 'destination_city_id'); - } + public function destinationTours() + { + return $this->hasMany(Tour::class, 'destination_city_id'); + } + + public function originTours() + { + return $this->hasMany(Tour::class, 'origin_city_id'); + } public function users() { diff --git a/app/Models/Flight.php b/app/Models/Flight.php index bb0299e..28db114 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -9,6 +9,8 @@ namespace App\Models; use App\Enums\FlightStatus; use App\Enums\FlightTripType; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Attributes\Scope; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; @@ -65,6 +67,28 @@ class Flight extends Model 'status', 'trip_type' ]; + #[Scope] + protected function applySort(Builder $query): void + { + switch (request()->input('sort')) { + case 'popularity': + $query->withCount('bookings') + ->orderByDesc('bookings_count'); + break; + + case 'lowest': + $query->orderBy('price'); + break; + + case 'highest': + $query->orderByDesc('price'); + break; + + default: + $query->latest('id'); // همه‌ی پروازها + break; + } + } public function airline() { diff --git a/app/Models/Tour.php b/app/Models/Tour.php index 4894742..03f1372 100644 --- a/app/Models/Tour.php +++ b/app/Models/Tour.php @@ -12,7 +12,7 @@ use Illuminate\Database\Eloquent\Model; /** * Class Tour - * + * * @property int $id * @property string $title * @property string $summary @@ -23,7 +23,7 @@ use Illuminate\Database\Eloquent\Model; * @property string $tour_type * @property Carbon $created_at * @property Carbon $updated_at - * + * * @property Flight $flight * @property City $city * @property Collection|TourDeparture[] $tourDepartures @@ -55,6 +55,10 @@ class Tour extends Model { return $this->belongsTo(Flight::class, 'file_id'); } + public function originCity() + { + return $this->belongsTo(City::class, 'origin_city_id'); + } public function city() { @@ -63,6 +67,11 @@ class Tour extends Model public function tourDepartures() { - return $this->hasMany(TourDeparture::class); + return $this->hasMany(TourDeparture::class , 'tour_id'); } + + public function file() + { + return $this->belongsTo(File::class, 'file_id'); + } }