From 27c83621fbdb0b0397ea180a9e79f8993718bbe8 Mon Sep 17 00:00:00 2001 From: mina-esn Date: Mon, 17 Aug 2026 16:39:25 +0330 Subject: [PATCH] feat: implement flight sorting and pagination --- app/Http/Controllers/FlightController.php | 14 ++++++---- app/Models/Flight.php | 33 +++++++++++++++++++++++ resources/views/flights/index.blade.php | 31 ++++++++++++--------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/FlightController.php b/app/Http/Controllers/FlightController.php index 9764077..7db20f3 100644 --- a/app/Http/Controllers/FlightController.php +++ b/app/Http/Controllers/FlightController.php @@ -10,15 +10,19 @@ class FlightController extends Controller { public function index(Request $request) { - $sort = $request->query('sort', 'default'); - $flights = Flight::with(['airline', 'origin_airport', 'airport']) - ->where('status', '=', FlightStatus::AVAILABLE) + $sort = $request->query('sort', 'default'); + $title = 'پروازها'; + + $flights = Flight::with(['airline', 'origin_airport.city', 'airport.city']) + ->where('status', FlightStatus::AVAILABLE) + ->filterFrom($request->input('from-destination')) + ->filterTo($request->input('to-destination')) + ->filterDepartureDate($request->input('journey-date')) ->applySort($sort) ->paginate(4) - ->appends(['sort' => $sort]); + ->withQueryString(); - $title = 'پروازها'; return view('flights.index', compact('title', 'flights', 'sort')); } diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 5cdad11..bf87071 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -9,6 +9,7 @@ namespace App\Models; use App\Enums\FlightStatus; use App\Enums\FlightTripType; use Carbon\Carbon; +use Hekmatinasser\Verta\Verta; use Illuminate\Database\Eloquent\Attributes\Scope; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -89,10 +90,42 @@ class Flight extends Model } } + #[Scope] + protected function filterFrom(Builder $query, ?string $cityName = null): void + { + if ($cityName) { + $query->whereHas('origin_airport.city', function (Builder $q) use ($cityName) { + $q->where('name', 'LIKE', "%$cityName%"); + }); + } + } + + #[Scope] + protected function filterTo(Builder $query, ?string $cityName = null): void + { + if ($cityName) { + $query->whereHas('airport.city', function (Builder $q) use ($cityName) { + $q->where('name', 'LIKE', "%$cityName%"); + }); + } + } + public function airline() { return $this->belongsTo(Airline::class); } + #[Scope] + protected function filterDepartureDate(Builder $query, ?string $dateInput = null): void + { + if (!$dateInput) { + return; + } + + + $date = Verta::parse($dateInput)->datetime(); + + $query->whereDate('departure_time', $date->format('Y-m-d')); + } public function origin_airport() { diff --git a/resources/views/flights/index.blade.php b/resources/views/flights/index.blade.php index ff4a903..2e49058 100644 --- a/resources/views/flights/index.blade.php +++ b/resources/views/flights/index.blade.php @@ -19,7 +19,7 @@