I'm encountering an error while trying to search using Meilisearch:
"Method Laravel\Scout\Builder::search does not exist."
Here's the setup I'm using:
- Laravel 11
- Sail
- Meilisearch
- Scout
- Laravel Actions
I followed the documentation to install Scout by running the command:
composer require laravel/scout
Then I published the configuration file using:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
I added the Searchable trait to my model like this:
use Laravel\Scout\Searchable;
class Organization extends Model {
use Searchable;
public function toSearchableArray() {
// All model attributes are made searchable
$array = $this->toArray();
// Add some additional attributes
$array['contact'] = $this->contact->toArray();
}
}
After that, I ran the command:
sail php artisan scout:import "Modules\Organizations\Models\Organization"
After this, I'm able to see the Meilisearch dashboard and see the data indexed.
In my attempt to search, I'm using the following code:
class GetOrganizations {
use AsInertia;
use SimpleIndexAction {
rules as traitRules;
getValidationData as traitGetValidationData;
}
protected $modelClass = Organization::class;
public function scope() {
$scope = Organization::search($this->search);
return $scope;
}
public function jsonResponse($organizations) {
return OrganizationResource::collection($organizations)->paginate(50);
}
public function htmlResponse($organizations) {
return inertia()->render('Organizations/Index', [
'title' => 'Organizations',
'organizations' => inertia()->lazy(fn() => $organizations->latest()->paginate(50)->withQueryString()),
'search' => request('search'),
]);
}
}
Upon loading the view, all the information is displayed correctly. However, when I try to search and send the parameter to the scope method, I receive the error
"Method Laravel\Scout\Builder::search does not exist."
I'm not sure what I'm missing here. Any help would be appreciated.
1 Answer 1
use Laravel\Scout\Searchable;
class Organization extends Model
{
use Searchable;
public function toSearchableArray()
{
// All model attributes are made searchable
$array = $this->toArray();
// Add some additional attributes
$array['contact'] = $this->contact->toArray();
return $array; // Seems to be missing in your code
}
}
You are supposed to return the toSearchableArray Documentation
However, when I try to search and send the parameter to the scope method, I receive the error
"Method Laravel\Scout\Builder::search does not exist."
public function scope()
{
$scope = Organization::search($this->search);
return $scope;
}
The function search would return \Laravel\Scout\Builder Reference
To see the list of functions exposed by \Laravel\Scout\Builder use this link
So it depends how the function scope is being used in your application. If your intention is to get the search results, you may use the function get
public function scope()
{
return Organization::search($this->search)->get();
}
Also, have you checked whether $this->search would contain the search query?
1 Comment
Explore related questions
See similar questions with these tags.