I am using Algolia as my search driver and I am simply trying to search my user records, based on the fact whether they have a relationship.
App\User::has('restaurant')->search('Chris')->get();
I utilize Laravel Scount and want to search all the users that have a restaurant and then list them, but the error I receive is
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::search()'
How can I search based on a relationship?
The best way would be to add a restaurant int attribute in your data sent to Algolia so you can rely on "where" attribute. I don't know what your data looks like so the code might need some modifications.
Where is very limited, it only with with equal operations on ints: https://laravel.com/docs/scout#where-clauses
where
methodFirst, override the toSearchableArray
method to have something like:
public function toSearchableArray()
{
$record = $this->toArray();
$record['has_resturant'] = (int) !empty($this->restaurants);
return $record;
}
Reindex everything with the command php artisan scout:import "App\User"
Add this package to your project: https://github.com/algolia/laravel-scout-algolia-macros
Create a restaurant_count
attribute
public function toSearchableArray()
{
$record = $this->toArray();
$record['resturant_count'] = count($this->restaurants);
return $record;
}
Reindex.
Search this way:
User::search('Chris')->with('filters' => 'restaurant_count>0')->get();
Let me know if that worked for you.