Merge branch 'master' into Development
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Laravel\Scout\Attributes\SearchUsingFullText;
|
||||
use Laravel\Scout\Searchable;
|
||||
|
||||
class Person extends Model
|
||||
@@ -64,6 +65,14 @@ protected static function booted()
|
||||
$person->nu = static::generateUniqueNu();
|
||||
}
|
||||
});
|
||||
|
||||
static::saving(function (Person $person) {
|
||||
$person->full_name_search = static::buildFullNameSearchPayload(
|
||||
$person->first_name,
|
||||
$person->last_name,
|
||||
$person->full_name
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
protected function makeAllSearchableUsing(Builder $query): Builder
|
||||
@@ -71,16 +80,20 @@ protected function makeAllSearchableUsing(Builder $query): Builder
|
||||
return $query->with(['addresses', 'phones', 'emails']);
|
||||
}
|
||||
|
||||
#[SearchUsingFullText(['full_name_search'], ['config' => 'simple'])]
|
||||
public function toSearchableArray(): array
|
||||
{
|
||||
return [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'full_name' => '',
|
||||
$columns = [
|
||||
'first_name' => (string) $this->first_name,
|
||||
'last_name' => (string) $this->last_name,
|
||||
'full_name' => (string) $this->full_name,
|
||||
'person_addresses.address' => '',
|
||||
'person_phones.nu' => '',
|
||||
'emails.value' => '',
|
||||
'full_name_search' => (string) $this->full_name_search,
|
||||
];
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function phones(): HasMany
|
||||
@@ -144,4 +157,43 @@ protected static function generateUniqueNu(): string
|
||||
|
||||
return $nu;
|
||||
}
|
||||
|
||||
protected static function buildFullNameSearchPayload(?string $firstName, ?string $lastName, ?string $fullName): string
|
||||
{
|
||||
$segments = collect([
|
||||
static::joinNameParts($firstName, $lastName),
|
||||
static::joinNameParts($lastName, $firstName),
|
||||
$fullName,
|
||||
])->filter();
|
||||
|
||||
if ($segments->isEmpty()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $segments
|
||||
->map(fn (string $segment): string => static::normalizeSegment($segment))
|
||||
->filter()
|
||||
->unique()
|
||||
->implode(' ');
|
||||
}
|
||||
|
||||
protected static function joinNameParts(?string $first, ?string $second): ?string
|
||||
{
|
||||
$parts = collect([$first, $second])->filter(fn ($value) => filled($value));
|
||||
|
||||
if ($parts->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $parts->implode(' ');
|
||||
}
|
||||
|
||||
protected static function normalizeSegment(?string $value): ?string
|
||||
{
|
||||
if (blank($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string) Str::of($value)->squish()->lower();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user