*/ protected $fillable = [ 'firstname', 'lastname', 'email' ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'remember_token' ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'last_connection' => 'date', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { // * EVENT CLOSURES. static::creating(function (self $user) { self::assertEmailIsUnique($user->email); }); static::updating(function (self $user) { self::assertEmailIsUnique($user->email, $user->id); }); } // * METHODS /** * Asserts using validation that the email is unique * * @param string $email * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If user email as already used. */ private static function assertEmailIsUnique(string $email, ?int $id = null): void { ToolboxHelper::assertFieldIsUnique((new self())->getTable(), 'email', $email, $id); } }