phpvms/app/Models/Aircraft.php

167 lines
3.8 KiB
PHP
Raw Normal View History

2017-06-09 09:37:51 +08:00
<?php
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\FuelCast;
use App\Models\Enums\AircraftStatus;
use App\Models\Traits\ExpensableTrait;
use App\Models\Traits\FilesTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Znck\Eloquent\Traits\BelongsToThrough;
2018-03-03 07:29:11 +08:00
/**
* @property int id
* @property mixed subfleet_id
* @property string airport_id The apt where the aircraft is
* @property string hub_id The apt where the aircraft is based
* @property string ident
* @property string name
* @property string icao
* @property string registration
* @property int flight_time
* @property float mtow
* @property float zfw
* @property string hex_code
* @property Airport airport
* @property Airport hub
2018-03-03 07:29:11 +08:00
* @property Subfleet subfleet
* @property int status
* @property int state
* @property Carbon landing_time
* @property float fuel_onboard
2018-03-03 07:29:11 +08:00
*/
class Aircraft extends Model
2017-06-09 09:37:51 +08:00
{
use BelongsToThrough;
use ExpensableTrait;
use FilesTrait;
use HasFactory;
2017-06-09 09:37:51 +08:00
public $table = 'aircraft';
2018-03-21 08:40:19 +08:00
protected $fillable = [
'subfleet_id',
'airport_id',
'hub_id',
'iata',
'icao',
'name',
'registration',
'hex_code',
2020-05-06 02:36:14 +08:00
'flight_time',
'mtow',
2018-01-31 00:04:50 +08:00
'zfw',
'fuel_onboard',
'status',
'state',
];
2017-06-09 09:37:51 +08:00
/**
* The attributes that should be casted to native types.
*/
protected $casts = [
'subfleet_id' => 'integer',
'mtow' => 'float',
'zfw' => 'float',
'flight_time' => 'float',
'fuel_onboard' => FuelCast::class,
'state' => 'integer',
];
2017-06-09 09:37:51 +08:00
/**
* Validation rules
*/
public static $rules = [
2018-03-23 06:17:37 +08:00
'subfleet_id' => 'required',
'name' => 'required',
'status' => 'required',
2018-03-23 06:17:37 +08:00
'registration' => 'required',
2020-09-04 03:14:26 +08:00
'mtow' => 'nullable|numeric',
'zfw' => 'nullable|numeric',
];
/**
* @return Attribute
*/
public function active(): Attribute
{
return Attribute::make(
get: fn ($_, $attr) => $attr['status'] === AircraftStatus::ACTIVE
);
}
/**
* @return Attribute
*/
public function icao(): Attribute
{
return Attribute::make(
set: fn ($value) => strtoupper($value)
);
}
/**
* @return Attribute
*/
public function ident(): Attribute
{
return Attribute::make(
get: fn ($_, $attrs) => $attrs['registration'].' ('.$attrs['icao'].')'
);
}
/**
* Return the landing time
*
* @return Attribute
*/
public function landingTime(): Attribute
{
return Attribute::make(
get: function ($_, $attrs) {
if (array_key_exists('landing_time', $attrs) && filled($attrs['landing_time'])) {
return new Carbon($attrs['landing_time']);
}
return $attrs['landing_time'];
}
);
}
/**
* foreign keys
*/
public function airline()
{
return $this->belongsToThrough(Airline::class, Subfleet::class);
}
2017-06-25 03:00:56 +08:00
public function airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'airport_id');
2017-06-25 03:00:56 +08:00
}
2017-06-10 14:50:00 +08:00
public function hub()
{
return $this->hasOne(Airport::class, 'id', 'hub_id');
}
public function pireps()
{
return $this->hasMany(Pirep::class, 'aircraft_id');
}
public function simbriefs()
{
return $this->hasMany(SimBrief::class, 'aircraft_id');
}
public function subfleet()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Subfleet::class, 'subfleet_id');
2017-06-10 14:50:00 +08:00
}
2017-06-09 09:37:51 +08:00
}