Reputation: 469
I'm using the laravel-scout-tntsearch-driver package for Laravel Scout. I have implemented it, and everything works fine. But now I want to do a relational search. I have cities that have many companies.
City.php
public function companies()
{
return $this->hasMany(Company::class);
}
Company.php
public function city()
{
return $this->belongsTo(City::class);
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title
];
}
Now the search is working only for all companies.
Company::search('bugs bunny')->get();
Also where clauses don't work here. I want something like this:
Route::get('/search/{city}', function (\App\City $city) {
$companies = $city->companies()->search('bugs bunny');
});
I think you got the idea. Thanks!
Upvotes: 5
Views: 478
Reputation: 574
First, I would move the logic to a controller. After doing that, you could have a method in the controller that implements the required search like this:
public function search(City $city){
$companiesInCity = Company::where('city_id', $city->id)->get('id')->toArray();
$companiesMatching = Company::search('bugs bunny')->whereIn('id', $companiesInCity)->get();
return view('search.result', [
'result' => $result
]);
}
And finally call the function from your routeing web.php.
Working in my dev env.
Hope this helps!
Upvotes: 0