Laravel Scout: Is there a way that I search in only a specific field?
At the moment this line is working fine:
$es = Element::search($q)->get();
But it searches title, shortdescription and description fields. I need it to only search in title field.
You just need to change your
toSearchableArraymethod from your model by adding the following code:Then call
php artisan scout:import "App\YourModel"to reindex the new records.Note:
$this->only('title', 'description')will search only for its title and description fields$this->user->only('name', 'email')will also search for its name and email from a related ModelSo you can retrieve the related data by adding
->load('user')in your search method like the following code:UPDATE
If you're trying to retrieve the data using ->paginate() method, you must need to load the relations separately:
Enjoy!