add separate flight_fields table and move over to flight_field_values

This commit is contained in:
Nabeel Shahzad
2018-03-20 13:06:06 -05:00
parent a9454c319a
commit 485c6e86bb
17 changed files with 147 additions and 45 deletions

View File

@@ -10,17 +10,17 @@ use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
/**
* @property Airline airline
* @property mixed flight_number
* @property mixed route_code
* @property mixed route_leg
* @property Collection fields
* @property string id
* @property Airline airline
* @property mixed flight_number
* @property mixed route_code
* @property mixed route_leg
* @property Collection field_values
*/
class Flight extends Model
{
use HashIdTrait;
public const ID_MAX_LENGTH = 12;
public $table = 'flights';
public $incrementing = false;
@@ -130,7 +130,7 @@ class Flight extends Model
*/
public function field($field_name): string
{
$field = $this->fields->where('name', $field_name)->first();
$field = $this->field_values->where('name', $field_name)->first();
if($field) {
return $field['value'];
}
@@ -168,9 +168,9 @@ class Flight extends Model
->withPivot('price', 'cost', 'capacity');
}
public function fields()
public function field_values()
{
return $this->hasMany(FlightFields::class, 'flight_id');
return $this->hasMany(FlightFieldValue::class, 'flight_id');
}
public function subfleets()

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Models;
use App\Interfaces\Model;
/**
* Class FlightField
* @property string name
* @property string slug
* @package App\Models
*/
class FlightField extends Model
{
public $table = 'flight_fields';
public $timestamps = false;
public $fillable = [
'name',
'slug',
'required',
];
protected $casts = [
'required' => 'boolean',
];
public static $rules = [
'name' => 'required',
];
/**
* When setting the name attribute, also set the slug
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
}
}

View File

@@ -5,12 +5,12 @@ namespace App\Models;
use App\Interfaces\Model;
/**
* Class FlightFields
* Class FlightFieldValue
* @package App\Models
*/
class FlightFields extends Model
class FlightFieldValue extends Model
{
public $table = 'flight_fields';
public $table = 'flight_field_values';
public $fillable = [
'flight_id',

View File

@@ -34,8 +34,6 @@ class Pirep extends Model
{
use HashIdTrait;
public const ID_MAX_LENGTH = 12;
public $table = 'pireps';
public $incrementing = false;

View File

@@ -2,6 +2,7 @@
namespace App\Models\Traits;
use App\Interfaces\Model;
use Hashids\Hashids;
trait HashIdTrait
@@ -10,11 +11,10 @@ trait HashIdTrait
* @return string
* @throws \Hashids\HashidsException
*/
protected static function createNewHashId(): string
final protected static function createNewHashId(): string
{
$hashids = new Hashids('', 12);
$hashids = new Hashids('', Model::ID_MAX_LENGTH);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
}
@@ -22,7 +22,7 @@ trait HashIdTrait
* Register callbacks
* @throws \Hashids\HashidsException
*/
protected static function bootHashIdTrait()
final protected static function bootHashIdTrait(): void
{
static::creating(function ($model) {
if (empty($model->id)) {