firstOrNew([ 'user_id' => $user->id, 'method' => $method ]); $mfaVal->setValue($value); $mfaVal->save(); } /** * Easily get the decrypted MFA value for the given user and method. */ public static function getValueForUser(User $user, string $method): ?string { /** @var MfaValue $mfaVal */ $mfaVal = static::query() ->where('user_id', '=', $user->id) ->where('method', '=', $method) ->first(); return $mfaVal ? $mfaVal->getValue() : null; } /** * Delete any stored MFA values for the given user and method. */ public static function deleteValuesForUser(User $user, string $method): void { static::query() ->where('user_id', '=', $user->id) ->where('method', '=', $method) ->delete(); } /** * Decrypt the value attribute upon access. */ protected function getValue(): string { return decrypt($this->value); } /** * Encrypt the value attribute upon access. */ protected function setValue($value): void { $this->value = encrypt($value); } }