Add airport search filter

This commit is contained in:
2026-08-22 15:00:55 +10:00
parent 91a6873b64
commit f31f1bc2fc
2 changed files with 58 additions and 18 deletions
+26 -14
View File
@@ -58,12 +58,17 @@ class SearchController extends Controller
{
$q = request('q', '');
$len = strlen($q);
$includeInactive = request()->boolean('include_inactive');
$showClosed = request()->boolean('show_closed');
$showNoCode = request()->boolean('show_no_code');
if ($len < 3) return [];
return Airport::with('region.country')
->when(!$includeInactive, fn($query) => $query->where('active', true))
->when(!$showClosed, fn($query) => $query->where('active', true))
->when(!$showNoCode, fn($query) => $query->where(function ($sub) {
$sub->whereNotNull('iata_code')
->orWhereNotNull('icao_code');
}))
->when($len === 3, fn($query) => $query->where('iata_code', 'ilike', $q))
->when($len >= 4, fn($query) => $query->where(function ($sub) use ($q, $len) {
$sub->when($len === 4, fn($s) => $s->where('icao_code', 'ilike', $q))
@@ -71,18 +76,25 @@ class SearchController extends Controller
->orWhere('municipality', 'ilike', "%{$q}%");
}))
->orderByRaw("
CASE
WHEN active THEN 0
ELSE 1
END
")
->orderByRaw("
CASE
WHEN icao_code = ? THEN 0
WHEN iata_code = ? THEN 1
ELSE 2
END
", [$q, $q])
CASE
WHEN active THEN 0
ELSE 1
END
")
->orderByRaw("
CASE
WHEN iata_code IS NOT NULL AND icao_code IS NOT NULL THEN 0
WHEN iata_code IS NOT NULL OR icao_code IS NOT NULL THEN 1
ELSE 2
END
")
->orderByRaw("
CASE
WHEN icao_code = ? THEN 0
WHEN iata_code = ? THEN 1
ELSE 2
END
", [$q, $q])
->limit(15)
->get(['id', 'name', 'municipality', 'iata_code', 'icao_code', 'region_id', 'active'])
->map(fn(Airport $airport) => [