Skip to content

Commit cb55a50

Browse files
Merge pull request #14 from ginkelsoft-development/fix/only-index-encrypted-fields
fix: only generate search tokens for fields with encrypted cast
2 parents adb9f10 + 73dbbe2 commit cb55a50

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Traits/HasEncryptedSearchIndex.php

Lines changed: 20 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;
@@ -279,6 +284,21 @@ public function scopeEncryptedPrefix(Builder $query, string $field, string $term
279284
}
280285

281286
/**
287+
* Check if a field has an encrypted cast.
288+
*
289+
* @param string $field
290+
* @return bool
291+
*/
292+
protected function hasEncryptedCast(string $field): bool
293+
{
294+
$casts = $this->getCasts();
295+
296+
if (!isset($casts[$field])) {
297+
return false;
298+
}
299+
300+
return str_contains(strtolower($casts[$field]), 'encrypted');
301+
}
282302
* Search for model IDs in Elasticsearch based on token(s).
283303
*
284304
* @param string $field

0 commit comments

Comments
 (0)