adding search stuff to Laravel model

This commit is contained in:
Brian 2022-05-18 09:20:51 -06:00
parent f809deb2da
commit d7d579d661
Signed by: brian
GPG Key ID: DE1A5390A3B84CD8

View File

@ -4,9 +4,11 @@ use App\Models\Traits\HasUidTrait;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\Prunable;
use Laravel\Scout\Searchable;
use HasUidTrait; use HasUidTrait;
use Prunable; use Prunable;
use Searchable;
/** @var string */ /** @var string */
protected \$table = ''; protected \$table = '';
@ -78,6 +80,58 @@ protected function pruning(): void
// //
} }
/**
* Get the value used to index the model.
*
* @since 1.0.0
*
* @return mixed
*/
public function getScoutKey()
{
return \$this->id;
}
/**
* Get the key name used to index the model.
*
* @since 1.0.0
*
* @return string
*/
public function getScoutKeyName(): string
{
return 'id';
}
/**
* Get the name of the index associated with the model.
*
* @since 1.0.0
*
* @return string
*/
public function searchableAs(): string
{
return 'models_index';
}
/**
* Get the indexable data array for the model.
*
* @since 1.0.0
*
* @return array
*/
public function toSearchableArray(): array
{
\$payload = \$this->toArray();
// modify here...
return \$payload;
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Accessors | Accessors
@ -95,7 +149,7 @@ protected function pruning(): void
protected function attrName(): Attribute protected function attrName(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn ($value, $attributes) => $attributes['foo'], get: fn (\$value, \$attributes) => \$attributes['foo'],
); );
} }