78 lines
2.7 KiB
PHP
78 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Aircraft;
|
|
use App\Models\Airline;
|
|
use App\Models\Airport;
|
|
use Illuminate\Http\Request;
|
|
|
|
class SearchController extends Controller
|
|
{
|
|
public function airlines(){
|
|
$q = request('q', '');
|
|
|
|
return Airline::orderByDesc('active')
|
|
->where(function ($query) use ($q) {
|
|
$query->where('name', 'ilike', "%{$q}%")
|
|
->orWhere('IATA_code', 'ilike', "%{$q}%")
|
|
->orWhere('ICAO_code', 'ilike', "%{$q}%");
|
|
})
|
|
->limit(50)
|
|
->get(['id', 'name', 'IATA_code', 'ICAO_code', 'logo'])
|
|
->map(fn($a) => [
|
|
'value' => $a->id,
|
|
'title' => "{$a->name} ({$a->IATA_code}/{$a->ICAO_code})",
|
|
])
|
|
->values();
|
|
}
|
|
|
|
public function aircraft()
|
|
{
|
|
$q = request('q', '');
|
|
$replacedQuery = str_replace(['A3', 'A2'], ['A-3', 'A-2'], $q);
|
|
|
|
return Aircraft::where('designator', 'ilike', "%{$q}%")
|
|
->orWhereRaw("CONCAT(manufacturer_code, ' ', model_full_name) ilike ?", ["%{$q}%"])
|
|
->orWhereRaw("CONCAT(manufacturer_code, ' ', model_full_name) ilike ?", ["%{$replacedQuery}%"])
|
|
->limit(200)
|
|
->orderBy('id', 'asc')
|
|
->get(['id', 'manufacturer_code', 'model_full_name', 'designator'])
|
|
->map(fn($a) => [
|
|
'value' => $a->id,
|
|
'title' => "{$a->manufacturer_code} {$a->model_full_name} ({$a->designator})",
|
|
])
|
|
->values();
|
|
}
|
|
|
|
public function airports()
|
|
{
|
|
$q = request('q', '');
|
|
$len = strlen($q);
|
|
|
|
if ($len < 3) return [];
|
|
|
|
return Airport::with('region.country')
|
|
->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))
|
|
->orWhere('name', 'ilike', "%{$q}%")
|
|
->orWhere('municipality', 'ilike', "%{$q}%");
|
|
})->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'])
|
|
->map(fn($a) => [
|
|
'value' => $a->id,
|
|
'title' => "{$a->municipality} / {$a->name} ({$a->iata_code}/{$a->icao_code})",
|
|
'country_code' => strtolower($a->region->country->code),
|
|
])
|
|
->values();
|
|
}
|
|
}
|