From 1a54aadc19ade8a14a6e58e887511896c0128fb8 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 21 Feb 2018 20:16:49 -0600 Subject: [PATCH] Make sure the subfleet name doesn't have spaces --- app/Models/Subfleet.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/Models/Subfleet.php b/app/Models/Subfleet.php index 2fe39ff6..6d7486f8 100644 --- a/app/Models/Subfleet.php +++ b/app/Models/Subfleet.php @@ -39,6 +39,31 @@ class Subfleet extends BaseModel 'type' => 'required', ]; + /** + * Modify some fields on the fly. Make sure the subfleet + * names don't have spaces in them. + */ + public static function boot() + { + parent::boot(); + + static::creating(function ($model) { + if (filled($model->name)) { + $model->name = str_replace(' ', '_', $model->name); + } + }); + + static::updating(function ($model) { + if (filled($model->name)) { + $model->name = str_replace(' ', '_', $model->name); + } + }); + } + + /** + * Relationships + */ + public function aircraft() { return $this->hasMany(Aircraft::class, 'subfleet_id');