*/ protected $fillable = [ 'breeder_code', 'inuav_codes', 'first_name', 'last_name', 'picture', 'picture_alt', 'picture_title', 'postal_code', 'department', 'place', 'testimony', 'approaches', 'latitude', 'longitude', 'geo_type', 'ocbStatus', 'ocbType', 'presentation_type', 'audit', 'valid_marketing_date', 'video_link', 'order', 'is_highlighted', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'inuav_codes' => 'array', 'approaches' => 'array', 'department' => Department::class, 'is_highlighted' => 'boolean', 'published' => 'boolean', 'display_random' => 'boolean', 'presentation_type' => PresentationType::class, 'ocbStatus' => OCBStatus::class, 'ocbType' => OCBType::class, 'geo_type' => GeoType::class, ]; /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $model) { self::setOrder($model); self::manageFields($model); }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); }); } // * METHODS /** * Manage model fields. */ private static function manageFields(self $model): void { $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); /** @var \App\Enums\Department|null $department */ $department = Str::of(strval($model->postal_code))->startsWith('97') ? Department::make(Str::of(strval($model->postal_code))->take(3)) : Department::make(Str::of(strval($model->postal_code))->take(2)); if (! is_null($department)) { $model->department = $department; } else { // Set a default department if the postal code is incorrect. $model->department = Department::ain; } } /** * Set order after the last element of the list. */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } }