Menno
Menno

Reputation: 155

How to use Laravel Scout & TNTSearch in combination with relational databases?

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.

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

Answers (1)

peterm
peterm

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

Related Questions