我正在使用Laravel-Scout-Scout-tntSearch-Driver软件包进行Laravel Scout。我已经实施了它,一切都很好。但是现在我想进行关系搜索。我有很多公司的城市。
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
];
}
现在搜索仅适用于所有公司。
Company::search('bugs bunny')->get();
也在这里无法使用条款。我想要这样的东西:
Route::get('/search/{city}', function (AppCity $city) {
$companies = $city->companies()->search('bugs bunny');
});
我认为你有这个想法。谢谢!
首先,我将逻辑移至控制器。执行此操作后,您可以在控制器中使用一种实现此类搜索的方法:
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
]);
}
最后从您的路由Web.php。
调用该功能在我的开发环境中工作
希望这会有所帮助!