*/ protected $fillable = [ 'label', 'picture', 'picture_alt', 'picture_title', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $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. * * @param self $model * @return void */ private static function manageFields(self $model): void { $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } // * RELATIONS /** * The pages that belong to the picto. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function commitmentPages(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(CommitmentPage::class); } /** * The products that belong to the picto. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(Product::class); } }