繁体   English   中英

使用 Laravel Scout 的 laravel-scout-tntsearch-driver 包进行关系搜索

[英]Relational search using laravel-scout-tntsearch-driver package for Laravel Scout

我正在为 Laravel Scout 使用laravel-scout-tntsearch-driver包。 我已经实现了它,一切正常。 但现在我想做一个关系搜索。 我有很多公司的城市。

城市.php

public function companies()
{
    return $this->hasMany(Company::class);
}

公司.php

public function city()
{
    return $this->belongsTo(City::class);
}

public function toSearchableArray()
{
    return [
        'id' => $this->id,
        'title' => $this->title
    ];
}

现在搜索只适用于所有公司。

Company::search('bugs bunny')->get();

where 子句在这里也不起作用。 我想要这样的东西:

Route::get('/search/{city}', function (\App\City $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 中调用该函数。

在我的开发环境中工作。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM