Reputation: 155
Let's say I have a table 'users' that I want to search with Laravel Scout. It has a lot of attributes, among which the hobby of the user.
The hobby of the user is not directly placed in the users
table,
but by a relational table. So a hobby-id is placed for each user in
the users
table.
Now when I search for football,
this is not indexed by Laravel
Scout. Only the id of the hobby football
is indexed, but that is of
course not how I want it.
Anyone knows how I can make sure that searching for football
returns the users that have football as a hobby?
Thanks for your time!
Upvotes: 3
Views: 1020
Reputation: 92805
You have a full control over what exactly is being indexed via toSearchableArray()
so that you can do something along the lines of
class User extends Model
{
use Searchable;
// ...
public function toSearchableArray()
{
return array_merge($this->toArray(), [
'hobby' => $this->hobby === null ? '' : $this->hobby->name;
]);
}
// ...
public function hobby()
{
return $this->belongsTo(Hobby::class);
}
}
Upvotes: 2