*/ protected $fillable = [ 'lastname', 'firstname', 'email', 'password', 'role', 'status', 'order', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'role' => AppUserRolesEnum::class ]; // * EVENTS /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::assertFieldIsUnique('email', $model->email); self::setOrder($model); self::updatePassword($model); }); static::updating(function (self $model) { self::updatePassword($model); }); } // * METHODS /** * Update password. * * @param self $model * @return void */ private static function updatePassword(self $model): void { if (Hash::needsRehash($model->password)) { $model->password = Hash::make($model->password); } } /** * 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): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id); } /** * 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()->where('tenant_id', \tenant() ?? Tenant::first()->getKey())->max('order') ) + 1; } // * RELATIONSHIPS /** * MorphToMany Directories * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function directories(): \Illuminate\Database\Eloquent\Relations\MorphToMany { return $this->morphToMany(Directory::class, 'directorable'); } }