Customize records
Scout Extended transforms your model into Algolia records with thetoSearchableArray
method. You can override this method to customize what data you want to index.
PHP
To know more about the
transform
method check the section Transformers.Relationships
Algolia doesn’t perform anyJOIN
operations.
All searchable data must exist in each record.
This means that model relationships aren’t indexed by default. If you want to index relationships within a record, you need to override the toSearchableArray
method in your model and include the relationships you want to index.
Add attributes from relations
For example, if you are indexing articles and each article has one author, all you need to do is add its full name or email.PHP
Many-to-many
If you want to index the entire relationship you can do it by loading them before calling thetoArray
method.
The resulting object has categories,
converted to an array by Laravel:
PHP
PHP
Algolia has a size limit per record,
so you should just index the data you need.
Update relations when parents or children change
After embedding information from a relation in your record, you might want to keep them up to date when you change the relation. Depending on the relationship type between your models, you have two solutions: using the$touches
property,
and listening for the saved
event.
The $touches
property
Laravel has a built-in feature to let the parent relationship know that one of their children has changed.
In a typical Article <=> Comment
example,
all you would need to do is add article
to the $touches
property.
PHP
This method only works with
belongsTo
and belongsToMany
relationships.The saved
event
If you use any other relationship,
listen to the saved
event and trigger the indexing.
PHP
Transformers
Some builder methods such aswhere
and whereBetween
require numeric values. For this reason, if toSearchableArray
isn’t defined, Scout Extended transforms, by default:
- Dates into
timestamps
- Numeric strings into
integers
orfloats
.
toSearchableArray
method:
PHP
Custom transformers
One of the primary benefits of creating aTransformer
class is the ability to type-hint any dependencies your transformer may need in its constructor. The declared dependencies are automatically resolved and injected into the transformer instance.
To write a transformer, create a new class that implements Algolia\ScoutExtended\Contracts\TransformerContract
. The transform
method should transform the given $value
as needed:
PHP
$array
into the transform
method and specifying
the transformers to apply.
PHP