name); self::manageFields($model); self::setOrder($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 /** * Undocumented function * * @param Model $model * @return void */ private static function manageFields(Model $model) { $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } /** * Set order after the last element of the list. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ private static function setOrder(Model $model): void { $model->order = \intval(self::query()->max('order')) + 1; } /** * Asserts using validation that the field is unique. * * @param string $nameField * @param mixed $valueField * @param integer|null $id * @return void */ private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id); } // * RELATIONSHIPS /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function storeRegions() { return $this->hasMany('App\Models\StoreRegion', 'group_id'); } }