2017-06-29 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2017-12-13 02:43:58 +08:00
|
|
|
use App\Models\Traits\HashId;
|
2017-06-29 08:54:05 +08:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Pirep
|
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
class Pirep extends BaseModel
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2017-12-13 02:43:58 +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'];
|
|
|
|
|
2017-12-10 11:21:49 +08:00
|
|
|
public $fillable = [
|
|
|
|
'user_id',
|
|
|
|
'flight_id',
|
|
|
|
'flight_number',
|
|
|
|
'route_code',
|
|
|
|
'route_leg',
|
|
|
|
'airline_id',
|
|
|
|
'aircraft_id',
|
2017-12-26 05:19:34 +08:00
|
|
|
'altitude',
|
2017-12-10 11:21:49 +08:00
|
|
|
'flight_time',
|
2017-12-26 05:19:34 +08:00
|
|
|
'planned_flight_time',
|
2017-12-10 11:21:49 +08:00
|
|
|
'dpt_airport_id',
|
|
|
|
'arr_airport_id',
|
|
|
|
'fuel_used',
|
|
|
|
'source',
|
|
|
|
'level',
|
|
|
|
'route',
|
|
|
|
'notes',
|
2017-12-20 10:19:36 +08:00
|
|
|
'state',
|
2017-12-10 11:21:49 +08:00
|
|
|
'status',
|
|
|
|
'raw_data',
|
|
|
|
];
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-10 11:21:49 +08:00
|
|
|
protected $casts = [
|
2017-12-26 05:19:34 +08:00
|
|
|
'id' => 'string',
|
|
|
|
'flight_time' => 'integer',
|
|
|
|
'planned_flight_time' => 'integer',
|
|
|
|
'level' => 'integer',
|
|
|
|
'altitude' => 'integer',
|
|
|
|
'fuel_used' => 'float',
|
|
|
|
'gross_weight' => 'float',
|
|
|
|
'source' => 'integer',
|
|
|
|
'state' => 'integer',
|
|
|
|
'status' => 'integer',
|
2017-12-10 11:21:49 +08:00
|
|
|
];
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validation rules
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
public static $rules = [
|
|
|
|
'dpt_airport_id' => 'required',
|
|
|
|
'arr_airport_id' => 'required',
|
|
|
|
];
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2017-12-04 05:29:34 +08:00
|
|
|
/**
|
2017-12-20 10:19:36 +08:00
|
|
|
* Get the flight ident, e.,g JBU1900
|
2017-12-04 05:29:34 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
public function getIdentAttribute()
|
2017-12-04 05:29:34 +08:00
|
|
|
{
|
|
|
|
$flight_id = $this->airline->code;
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($this->flight_id) {
|
2017-12-04 05:29:34 +08:00
|
|
|
$flight_id .= $this->flight->flight_number;
|
|
|
|
} else {
|
|
|
|
$flight_id .= $this->flight_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flight_id;
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
/**
|
|
|
|
* Foreign Keys
|
|
|
|
*/
|
2017-12-27 05:26:12 +08:00
|
|
|
|
|
|
|
public function acars()
|
|
|
|
{
|
2017-12-28 06:47:22 +08:00
|
|
|
return $this->hasMany('App\Models\Acars', 'pirep_id')
|
|
|
|
->orderBy('created_at', 'desc');
|
2017-12-27 05:26:12 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 10:25:48 +08:00
|
|
|
public function aircraft()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Aircraft', 'aircraft_id');
|
|
|
|
}
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2017-07-23 22:45:33 +08:00
|
|
|
public function airline()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Airline', 'airline_id');
|
|
|
|
}
|
|
|
|
|
2017-06-30 04:50:16 +08:00
|
|
|
public function arr_airport()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2017-06-30 04:50:16 +08:00
|
|
|
return $this->belongsTo('App\Models\Airport', '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
|
|
|
{
|
2017-06-30 04:50:16 +08:00
|
|
|
return $this->belongsTo('App\Models\Airport', 'dpt_airport_id');
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
2017-08-02 23:17:54 +08:00
|
|
|
public function comments()
|
|
|
|
{
|
2017-12-28 06:47:22 +08:00
|
|
|
return $this->hasMany('App\Models\PirepComment', '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
|
|
|
{
|
2017-06-30 04:50:16 +08:00
|
|
|
return $this->hasMany('App\Models\PirepFieldValues', '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
|
|
|
{
|
2017-06-30 04:50:16 +08:00
|
|
|
return $this->belongsTo('App\Models\Flight', 'flight_id');
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
public function pilot()
|
|
|
|
{
|
|
|
|
return $this->user();
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:47:22 +08:00
|
|
|
/**
|
|
|
|
* 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()
|
2017-12-21 01:28:21 +08:00
|
|
|
{
|
2017-12-28 06:47:22 +08:00
|
|
|
return $this->hasOne('App\Models\Acars', 'pirep_id')->latest();
|
2017-12-21 01:28:21 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 04:50:16 +08:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\User', 'user_id');
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
}
|