mrdigex's avatar

Sail + Scout + Meilisearch: Search Method doesn't exist

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 the 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.

0 likes
0 replies

Please or to participate in this conversation.