Installation:
composer require ijeffro/laravel-aircrafts
(Note: Use dev-master for Laravel 5 compatibility if needed.)
Register Service Provider & Facade:
Add to config/app.php:
'providers' => [
IJeffro\Aircrafts\AircraftsServiceProvider::class,
],
'aliases' => [
'Aircrafts' => IJeffro\Aircrafts\AircraftsFacade::class,
]
Publish Config (Optional):
php artisan vendor:publish --provider="IJeffro\Aircrafts\AircraftsServiceProvider"
(Configures table name for aircraft data.)
First Use Case: Fetch an aircraft by IATA code in a controller:
use Aircrafts;
$aircraft = Aircrafts::findByIata('A320');
dd($aircraft->name); // Output: Airbus A320
Data Retrieval:
$aircraft = Aircrafts::findByIata('B737');
$boeingAircrafts = Aircrafts::where('manufacturer', 'Boeing')->get();
$allAircrafts = Aircrafts::all();
Integration with Eloquent Models:
Add a relationship to a Flight model:
public function aircraft()
{
return $this->belongsTo(Aircraft::class, 'aircraft_iata');
}
Then fetch:
$flight->aircraft->name; // e.g., "Boeing 747"
Validation: Use the facade to validate IATA codes in Form Requests:
public function rules()
{
return [
'aircraft_iata' => 'required|exists:aircrafts,iata_code',
];
}
Caching: Cache frequent queries (e.g., manufacturer lists):
$manufacturers = Cache::remember('aircraft_manufacturers', now()->addHours(1), function () {
return Aircrafts::distinct()->pluck('manufacturer');
});
Dynamic Filtering: Combine with Laravel Scout for search:
use Aircrafts;
$results = Aircrafts::search('787')->get();
API Responses: Normalize aircraft data for APIs:
return response()->json([
'data' => Aircrafts::findByIata($request->aircraft)->toArray(),
]);
Seeding: Publish and seed the aircraft database:
php artisan vendor:publish --provider="IJeffro\Aircrafts\AircraftsServiceProvider" --tag=migrations
php artisan migrate
Laravel 5 Compatibility:
dev-master branch is explicitly for Laravel 5. For Laravel 6+, check for updates or fork the package.composer require ijeffro/laravel-aircrafts:dev-master only if targeting Laravel 5.Missing Migrations:
php artisan vendor:publish --provider="IJeffro\Aircrafts\AircraftsServiceProvider" --tag=migrations
php artisan migrate
Case Sensitivity:
// Correct:
Aircrafts::findByIata('A380');
// Incorrect (may return null):
Aircrafts::findByIata('a380');
Data Freshness:
Check Published Config:
Verify config/aircrafts.php for custom table names or other settings:
'table' => 'aircrafts', // Default; adjust if needed
Query Logs: Enable Laravel query logging to debug slow queries:
DB::enableQueryLog();
$aircraft = Aircrafts::findByIata('A320');
dd(DB::getQueryLog());
Facade vs. Direct Model:
Prefer the facade for consistency, but use the underlying model (\IJeffro\Aircrafts\Models\Aircraft) for advanced queries:
use IJeffro\Aircrafts\Models\Aircraft;
$customQuery = Aircraft::where('range_km', '>', 10000)->get();
Add Custom Fields:
Extend the aircrafts table migration:
Schema::table('aircrafts', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
Create a Decorator: Override the facade to add custom logic:
class CustomAircraftsFacade extends AircraftsFacade
{
public static function getByRange($minRange)
{
return parent::where('range_km', '>=', $minRange)->get();
}
}
Register it in config/app.php.
Localization: Add translated aircraft names by publishing the language files:
php artisan vendor:publish --provider="IJeffro\Aircrafts\AircraftsServiceProvider" --tag=lang
Then extend resources/lang/en/aircrafts.php.
How can I help you explore Laravel packages today?