feat: implement flight sorting and pagination
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
<div class="search-box flight-search">
|
||||
<div class="search-form">
|
||||
<form action="#">
|
||||
<form action="{{ route('flights.index') }}" method="GET">
|
||||
|
||||
<div class="flight-type">
|
||||
<div class="form-check form-check-inline">
|
||||
@@ -285,6 +285,9 @@
|
||||
<h5>{{$flights->total()}} نتیجه پیداشد</h5>
|
||||
<div class="col-md-3 booking-sort-box">
|
||||
<form method="GET" action="{{ route('flights.index') }}" class="flight-sort-form">
|
||||
<input type="hidden" name="from" value="{{ request('from') }}">
|
||||
<input type="hidden" name="to" value="{{ request('to') }}">
|
||||
<input type="hidden" name="departure_date" value="{{ request('departure_date') }}">
|
||||
<select name="sort" class="form-select" onchange="this.form.submit()">
|
||||
<option
|
||||
value="default" {{ request('sort') == 'default' ? 'selected' : '' }}>
|
||||
@@ -326,7 +329,7 @@
|
||||
<i class="fal fa-plane-departure"></i>
|
||||
</div>
|
||||
<div class="start-time-info">
|
||||
<h6 class="start-time-text">{{$flight->departure_time}}</h6>
|
||||
<h6 class="start-time-text">{{ verta($flight->departure_time)->format('Y/m/d') }}</h6>
|
||||
<span
|
||||
class="flight-destination">{{$flight->origin_airport->city->name}}</span>
|
||||
</div>
|
||||
@@ -339,7 +342,7 @@
|
||||
<i class="fal fa-plane-arrival"></i>
|
||||
</div>
|
||||
<div class="start-time-info">
|
||||
<h6 class="end-time-text">{{$flight->arrival_time}}</h6>
|
||||
<h6 class="end-time-text">{{ verta($flight->arrival_time)->format('Y/m/d') }}</h6>
|
||||
<span
|
||||
class="flight-destination">{{$flight->airport->city->name}}</span>
|
||||
</div>
|
||||
@@ -480,6 +483,7 @@
|
||||
<div class="alert alert-warning">هیچ پروازی یافت نشد</div>
|
||||
@endforelse
|
||||
</div>
|
||||
@if($flights->hasPages())
|
||||
<div class="pagination-area">
|
||||
<div aria-label="Page navigation example">
|
||||
{{ $flights->links() }}
|
||||
@@ -491,6 +495,7 @@
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user