'array' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { self::manageFields($model); }); static::updating(function (self $model) { self::manageFields($model); }); } // * METHODS /** * Undocumented function * * @param Model $model * @return void */ private static function manageFields(Model $model) { static::assertTitleIsUnique($model->title, $model->id); $model->banner = FileStorageHelper::storeFile($model, $model->banner); $model->picture = FileStorageHelper::storeFile($model, $model->picture); $model->slug = Str::slug($model->title); } /** * Asserts using validation that the title is unique * * @param mixed $title * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If an actu title already exists. */ private static function assertTitleIsUnique($title, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id); } }