*/ protected $fillable = [ 'title', 'picture', 'picture_alt', 'picture_title', 'published', 'order', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'published' => 'bool', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::setOrder($model); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); }); static::updating(function (self $model) { $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); }); } // * METHODS /** * Set order after the last element of the list. * * @param self $model * @return void */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } // * RELATIONS /** * The products that belong to the commitment. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(Product::class); } /** * The page that belong to the commitment. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function explorerpage(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(Explorerpage::class); } }