0

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:

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.

asked Aug 28, 2024 at 2:15

1 Answer 1

0
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?

answered Aug 31, 2024 at 9:11
Sign up to request clarification or add additional context in comments.

1 Comment

Just to address my issue, it was simple - I forgot to include the return statement in the toSearchableArray method, if is it's important for others.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.