Installation:
composer require ijeffro/laravel-airports dev-master
Add to config/app.php:
'providers' => [
ijeffro\Airports\AirportsServiceProvider::class,
],
'aliases' => [
'Airports' => ijeffro\Airports\AirportsFacade::class,
]
Publish Config (Optional):
php artisan vendor:publish --provider="ijeffro\Airports\AirportsServiceProvider"
(Modify config/airports.php if needed, e.g., for custom table names.)
First Use Case: Fetch an airport by IATA code:
$airport = Airports::getByIata('JFK');
// Returns: ['iata' => 'JFK', 'name' => 'John F Kennedy Intl', 'city' => 'New York', ...]
Querying Airports:
Airports::getByIata('LAX')Airports::getByName('Heathrow')Airports::all() (returns Collection of arrays)Airports::whereCountry('US')Integration with Eloquent:
Add a relationship to a Flight model:
public function departureAirport()
{
return $this->belongsTo(Airport::class, 'departure_iata', 'iata');
}
Validation: Use in Form Requests:
$this->validate($request, [
'departure_iata' => 'required|exists:airports,iata',
]);
Caching: Cache frequent queries (e.g., country dropdowns):
$countries = Cache::remember('airport_countries', now()->addHours(1), function() {
return Airports::pluck('country')->unique()->sort();
});
Custom Queries: Extend the facade or service provider to add methods:
// In a service class
public function getAirportsByRegion($region)
{
return Airports::where('region', $region)->get();
}
API Integration: Use in API responses:
return AirportResource::collection(Airports::whereCountry($request->country)->get());
Seeding: Preload airports in a seeder:
public function run()
{
Airport::insert(Airports::all()->toArray());
}
Laravel 5 Compatibility:
dev-master branch is Laravel 5-only. For Laravel 8+, check for updated forks or manual migration.Case Sensitivity:
'jfk' ≠ 'JFK').$airport = Airports::getByIata(strtoupper($request->iata));
Missing Data:
city, country, or region fields.$city = $airport['city'] ?? 'N/A';
Performance:
Airports::all() loads ~10,000 records. Cache aggressively or paginate:
$airports = Airports::paginate(50);
Table Structure:
Run php artisan schema:dump to inspect the airports table schema. Common columns:
iata (primary key), name, city, country, region, latitude, longitude.Missing Facade Methods:
Check the AirportsFacade class for available methods. Extend via:
// In a service provider
$this->app->bind('Airports', function($app) {
return new CustomAirportsService($app['Airports']);
});
Custom Fields: Add computed properties to airport data:
$airportWithDistance = Airports::getByIata('JFK')->merge([
'distance_km' => $this->calculateDistance($airport, $userLocation),
]);
Geospatial Queries:
Use Laravel Scout or a package like spatie/laravel-geocoding for distance-based searches:
$nearbyAirports = Airport::near($request->lat . ',' . $request->lng)->get();
Localization: Override airport names/cities for multilingual apps:
$translatedAirport = Airports::getByIata('CDG')->merge([
'name' => __("airports.{$airport['iata']}.name"),
]);
Webhooks/Updates: Subscribe to the OpenFlights API for periodic updates and trigger a Laravel job to refresh the database:
// In a scheduled job
public function handle()
{
Airport::truncate();
Airport::insert(Airports::all()->toArray());
}
How can I help you explore Laravel packages today?