Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhymesey's avatar

Laravel scout testing with relations

I am trying to figure out the best way to test Laravel Scout searches. I am using Meilisearch for local development of the app which is fine, but wanted a driver I could use for the local testing and for CI pipeline testing. I tried using the Laravel scout array driver but it doesn't seem to work with whenIn().

Here is an example of my searchable Services model. The languages are a relation, that are uploaded to meilisearch as an array.

public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
            'languages' => $this->languages()->pluck('language')->toArray(),
        ];
}

I can use the whereIn() method on my search to find the Services with the matching language:

Service::search($request->get('text'))
		->whereIn('languages',  ['French', 'English'])
		->orderBy('name')
		->paginate(10);

This works as expected with meilisearch, but when I try the test with the array driver it doesn't filter the results as expected. For example, it returns a service with German as a language, but doesn't have either French or English set.

I am unsure if this is because they are relations of the service model, rather than fields in the Service model?

0 likes
0 replies

Please or to participate in this conversation.