'integer', 'days' => 'integer', 'level' => 'integer', 'distance' => DistanceCast::class, 'flight_time' => 'integer', 'start_date' => 'date', 'end_date' => 'date', 'load_factor' => 'double', 'load_factor_variance' => 'double', 'pilot_pay' => 'float', 'has_bid' => 'boolean', 'route_leg' => 'integer', 'active' => 'boolean', 'visible' => 'boolean', ]; public static $rules = [ 'airline_id' => 'required|exists:airlines,id', 'flight_number' => 'required', 'callsign' => 'string|max:4|nullable', 'route_code' => 'nullable', 'route_leg' => 'nullable', 'dpt_airport_id' => 'required|exists:airports,id', 'arr_airport_id' => 'required|exists:airports,id', 'load_factor' => 'nullable|numeric', 'load_factor_variance' => 'nullable|numeric', 'level' => 'nullable', ]; /** * Return all of the flights on any given day(s) of the week * Search using bitmasks * * @param Days[] $days List of the enumerated values * * @return Flight */ public static function findByDays(array $days) { /** @noinspection DynamicInvocationViaScopeResolutionInspection */ $flights = self::where('active', true); foreach ($days as $day) { $flights = $flights->where('days', '&', $day); } return $flights; } /** * Get the flight ident, e.,g JBU1900/C.nn/L.yy */ public function ident(): Attribute { return Attribute::make( get: function ($_, $attrs) { $flight_id = optional($this->airline)->code; $flight_id .= $this->flight_number; if (filled($this->route_code)) { $flight_id .= '/C.'.$this->route_code; } if (filled($this->route_leg)) { $flight_id .= '/L.'.$this->route_leg; } return $flight_id; } ); } /** * @param $day * * @return bool */ public function on_day($day): bool { return ($this->days & $day) === $day; } /** * Return a custom field value * * @param $field_name * * @return string */ public function field($field_name): string { $field = $this->field_values->where('name', $field_name)->first(); if ($field) { return $field['value']; } return ''; } /** * Set the days parameter. If an array is passed, it's * AND'd together to create the mask value * * @return Attribute */ public function days(): Attribute { return Attribute::make( set: function ($value) { if (\is_array($value)) { $value = Days::getDaysMask($value); } return $value; } ); } /* * Relationships */ public function airline() { return $this->belongsTo(Airline::class, 'airline_id'); } public function dpt_airport() { return $this->belongsTo(Airport::class, 'dpt_airport_id'); } public function arr_airport() { return $this->belongsTo(Airport::class, 'arr_airport_id'); } public function alt_airport() { return $this->belongsTo(Airport::class, 'alt_airport_id'); } public function fares() { return $this->belongsToMany(Fare::class, 'flight_fare') ->withPivot('price', 'cost', 'capacity'); } public function field_values() { return $this->hasMany(FlightFieldValue::class, 'flight_id'); } public function simbrief() { // id = key from table, flight_id = reference key return $this->belongsTo(SimBrief::class, 'id', 'flight_id'); } public function subfleets() { return $this->belongsToMany(Subfleet::class, 'flight_subfleet'); } }