phpvms/app/Models/Pirep.php

167 lines
3.8 KiB
PHP
Raw Normal View History

2017-06-29 08:54:05 +08:00
<?php
namespace App\Models;
use App\Models\Enums\AcarsType;
use App\Models\Traits\HashId;
2017-06-29 08:54:05 +08:00
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Pirep
*
* @package App\Models
*/
class Pirep extends BaseModel
2017-06-29 08:54:05 +08:00
{
use HashId;
2017-06-29 08:54:05 +08:00
use SoftDeletes;
public $table = 'pireps';
2017-07-05 02:57:08 +08:00
public $incrementing = false;
2017-06-29 08:54:05 +08:00
protected $dates = ['deleted_at'];
public $fillable = [
'user_id',
'flight_id',
'flight_number',
'route_code',
'route_leg',
'airline_id',
'aircraft_id',
'altitude',
'flight_time',
'planned_flight_time',
'dpt_airport_id',
'arr_airport_id',
'fuel_used',
'source',
'level',
'route',
'notes',
'state',
'status',
'raw_data',
];
2017-06-29 08:54:05 +08:00
protected $casts = [
'flight_time' => 'integer',
'planned_flight_time' => 'integer',
'level' => 'integer',
'altitude' => 'integer',
'fuel_used' => 'float',
'gross_weight' => 'float',
'source' => 'integer',
'state' => 'integer',
'status' => 'integer',
];
2017-06-29 08:54:05 +08:00
public static $rules = [
2017-12-31 04:37:10 +08:00
'flight_number' => 'required',
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
'notes' => 'nullable',
'route' => 'nullable',
];
2017-06-29 08:54:05 +08:00
2017-12-04 05:29:34 +08:00
/**
* Get the flight ident, e.,g JBU1900
2017-12-04 05:29:34 +08:00
* @return string
*/
public function getIdentAttribute()
2017-12-04 05:29:34 +08:00
{
$flight_id = $this->airline->code;
if(!empty($this->flight_number)) {
2017-12-04 05:29:34 +08:00
$flight_id .= $this->flight_number;
} else {
if ($this->flight_id) {
$flight_id .= $this->flight->flight_number;
}
2017-12-04 05:29:34 +08:00
}
return $flight_id;
}
2017-06-29 08:54:05 +08:00
/**
* Foreign Keys
*/
public function acars()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::FLIGHT_PATH)
->orderBy('created_at', 'asc');
}
public function acars_logs()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::LOG)
->orderBy('created_at', 'asc');
}
2018-01-02 06:01:01 +08:00
public function acars_route()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
2018-01-02 06:01:01 +08:00
->where('type', AcarsType::ROUTE)
2018-01-05 09:33:23 +08:00
->orderBy('order', 'asc');
2018-01-02 06:01:01 +08:00
}
2017-07-03 10:25:48 +08:00
public function aircraft()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Aircraft::class, 'aircraft_id');
2017-07-03 10:25:48 +08:00
}
2017-06-29 08:54:05 +08:00
2017-07-23 22:45:33 +08:00
public function airline()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airline::class, 'airline_id');
2017-07-23 22:45:33 +08:00
}
2017-06-30 04:50:16 +08:00
public function arr_airport()
2017-06-29 08:54:05 +08:00
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'arr_airport_id');
2017-06-29 08:54:05 +08:00
}
2017-06-30 04:50:16 +08:00
public function dpt_airport()
2017-06-29 08:54:05 +08:00
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'dpt_airport_id');
2017-06-29 08:54:05 +08:00
}
2017-08-02 23:17:54 +08:00
public function comments()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(PirepComment::class, 'pirep_id')
->orderBy('created_at', 'desc');
2017-07-24 02:07:41 +08:00
}
2017-06-30 04:50:16 +08:00
public function fields()
2017-06-29 08:54:05 +08:00
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(PirepFieldValues::class, 'pirep_id');
2017-06-29 08:54:05 +08:00
}
2017-06-30 04:50:16 +08:00
public function flight()
2017-06-29 08:54:05 +08:00
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Flight::class, 'flight_id');
2017-06-30 04:50:16 +08:00
}
public function pilot()
{
return $this->user();
}
/**
* Relationship that holds the current position, but limits the ACARS
* relationship to only one row (the latest), to prevent an N+! problem
*/
public function position()
{
2018-01-08 23:22:12 +08:00
return $this->hasOne(Acars::class, 'pirep_id')
->where('type', AcarsType::FLIGHT_PATH)
->latest();
}
2017-06-30 04:50:16 +08:00
public function user()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(User::class, 'user_id');
2017-06-29 08:54:05 +08:00
}
}