*/ protected $fillable = [ 'year', 'title', 'description', 'picture', 'picture_alt', 'picture_title', 'video_id', 'picto', 'picto_alt', 'picto_title', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'year' => 'integer', 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $historicalMilestone) { self::setImage($historicalMilestone, 'picture'); self::setImage($historicalMilestone, 'picto'); }); static::updating(function (self $historicalMilestone) { self::setImage($historicalMilestone, 'picture'); self::setImage($historicalMilestone, 'picto'); }); static::updated(function (self $historicalMilestone) { FileStorageHelper::removeOldFile($historicalMilestone, 'picture'); FileStorageHelper::removeOldFile($historicalMilestone, 'picto'); }); static::deleted(function (self $historicalMilestone) { FileStorageHelper::removeFile($historicalMilestone, 'picture'); FileStorageHelper::removeFile($historicalMilestone, 'picto'); }); } // * METHODS /** * Set model's published date. * * @param self $historicalMilestone * @param string $field * @return void */ private static function setImage(self $historicalMilestone, string $field): void { if (is_null($historicalMilestone->{$field})) { $historicalMilestone->{$field . "_alt"} = null; $historicalMilestone->{$field . "_title"} = null; } else { $historicalMilestone->{$field} = FileStorageHelper::storeFile( $historicalMilestone, $historicalMilestone->{$field}, true ); } } }