fix: fix flight list and make it dynamic
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user