AppUserRolesEnum::class ]; // * EVENTS /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { self::assertFieldIsUnique('email', $model->email); self::setOrder($model); $model->password = Hash::make($model->password); }); static::updating(function (self $model) { if (Hash::needsRehash($model->password)) { $model->password = Hash::make($model->password); } }); } // * METHODS /** * Asserts using validation that the field is unique. * * @param string $nameField * @param string $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); } /** * 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; } // * RELATIONSHIPS /** * MorphToMany Directories * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function directories() { return $this->morphToMany(Directory::class, 'directorable'); } }