Skip to content

Commit d03cf39

Browse files
fix: only generate search tokens for fields with encrypted cast
Added validation to skip token generation for fields that don't have an encrypted cast. This prevents unnecessary indexing and ensures that only truly encrypted data gets searchable tokens in the index. Changes: - Added hasEncryptedCast() helper method to check field cast type - Updated updateSearchIndex() to validate encrypted cast before processing - Improved efficiency by skipping non-encrypted fields early
1 parent 6975c73 commit d03cf39

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Traits/HasEncryptedSearchIndex.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function updateSearchIndex(): void
7878
$rows = [];
7979

8080
foreach ($config as $field => $modes) {
81+
// Skip fields that don't have an encrypted cast
82+
if (!$this->hasEncryptedCast($field)) {
83+
continue;
84+
}
85+
8186
$raw = (string) $this->getAttribute($field);
8287
if ($raw === '') {
8388
continue;
@@ -264,6 +269,23 @@ public function scopeEncryptedPrefix(Builder $query, string $field, string $term
264269
});
265270
}
266271

272+
/**
273+
* Check if a field has an encrypted cast.
274+
*
275+
* @param string $field
276+
* @return bool
277+
*/
278+
protected function hasEncryptedCast(string $field): bool
279+
{
280+
$casts = $this->getCasts();
281+
282+
if (!isset($casts[$field])) {
283+
return false;
284+
}
285+
286+
return str_contains(strtolower($casts[$field]), 'encrypted');
287+
}
288+
267289
/**
268290
* Resolve the encrypted search configuration for this model.
269291
*

0 commit comments

Comments
 (0)