我有一个多对多的关系,我会用 laravel scout (meilisearch-driver) 对这种关系进行搜索。
我的模型:
受益人.php
<?php
namespace App\Models;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Beneficiary extends Model
{
use Searchable;
protected $table = 'beneficiaries';
public function contacts() {
return $this->belongsToMany('App\Models\Contact', 'beneficiary_contacts', 'beneficiary_id', 'contact_id');
}
protected function makeAllSearchableUsing($query) {
return $query->with('contacts');
}
}
联系方式.php
<?php
namespace App\Models;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use Searchable;
protected $table = 'contacts';
public function beneficiaries() {
return $this->belongsToMany('App\Models\Beneficiary', 'beneficiary_contacts', 'contact_id', 'beneficiary_id');
}
protected function makeAllSearchableUsing($query) {
return $query->with('beneficiaries');
}
}
我需要通过搜索字符串找到特定“受益人”的“联系人”。我在我的控制器中试过这个:
public function getBeneficiaryContactsBySearch(Request $request, $id) {
...
$contacts = Beneficiary::findOrFail($id)->contacts()->search($search)->paginate($request->per_page);
...
}
但我得到错误“ Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::search()
”。
有没有办法用 laravel scout 做到这一点?
谢谢。