diff --git a/app/Http/Controllers/FlightController.php b/app/Http/Controllers/FlightController.php index 0c30237..9764077 100644 --- a/app/Http/Controllers/FlightController.php +++ b/app/Http/Controllers/FlightController.php @@ -8,14 +8,18 @@ use Illuminate\Http\Request; class FlightController extends Controller { - public function index() + public function index(Request $request) { - $flights =Flight::applySort() - ->where('status' , '=' , FlightStatus::AVAILABLE) - ->paginate() - ->withQueryString(); + $sort = $request->query('sort', 'default'); + + $flights = Flight::with(['airline', 'origin_airport', 'airport']) + ->where('status', '=', FlightStatus::AVAILABLE) + ->applySort($sort) + ->paginate(4) + ->appends(['sort' => $sort]); + $title = 'پروازها'; - return view('flights.index' , compact('title' , 'flights')); + return view('flights.index', compact('title', 'flights', 'sort')); } public function show(Flight $flight) diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 28db114..5cdad11 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -68,12 +68,11 @@ class Flight extends Model 'trip_type' ]; #[Scope] - protected function applySort(Builder $query): void + protected function applySort(Builder $query, ?string $sort = null): void { - switch (request()->input('sort')) { + switch ($sort) { case 'popularity': - $query->withCount('bookings') - ->orderByDesc('bookings_count'); + $query->withCount('bookings')->orderByDesc('bookings_count'); break; case 'lowest': @@ -85,7 +84,7 @@ class Flight extends Model break; default: - $query->latest('id'); // همهی پروازها + $query->orderByDesc('id'); break; } } diff --git a/resources/views/flights/index.blade.php b/resources/views/flights/index.blade.php index d5c1deb..ff4a903 100644 --- a/resources/views/flights/index.blade.php +++ b/resources/views/flights/index.blade.php @@ -23,19 +23,22 @@
فرودگاه بین المللی مشهد @@ -65,7 +69,8 @@
فرودگاه بین المللی تهران
@@ -77,7 +82,8 @@2-12 سال
در زیر 2 سال
فرودگاه بین المللی مشهد @@ -193,7 +225,8 @@
فرودگاه بین المللی تهران
@@ -205,7 +238,8 @@
-