fix: fix flight list and make it dynamic

This commit is contained in:
2026-08-17 11:51:28 +03:30
parent 33c445eb02
commit 5350cd1742
3 changed files with 244 additions and 229 deletions
+10 -6
View File
@@ -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)
+4 -5
View File
@@ -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;
}
}