I have a many-to-many relationship and I would to perform a search on that relationship with laravel scout (meilisearch-driver).
My models:
Beneficiary.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');
}
}
Contact.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');
}
}
I need to find "contacts" of a specific "beneficiary" by search string. I tried this in my controller:
public function getBeneficiaryContactsBySearch(Request $request, $id) {
...
$contacts = Beneficiary::findOrFail($id)->contacts()->search($search)->paginate($request->per_page);
...
}
But I get the error "Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::search()
".
Is there a way to do that with laravel scout ?
Thanks.