Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] - Collection Engine: add support for non-scalar values #528

Conversation

macbookandrew
Copy link
Contributor

The Algolia and MeiliSearch engines both allow searchable data to be non-scalar values (e.g., an array of ingredients or genres).

The Collection engine, however, expects all values to be scalar, and during search, throws a TypeError if a model’s toSearchableArray() returns any non-scalar key.

TypeError: mb_strtolower(): Argument #1 ($string) must be of type string, array given in /path/to/project/vendor/laravel/framework/src/Illuminate/Support/Str.php:361
Stack trace:
#0 /path/to/project/vendor/laravel/framework/src/Illuminate/Support/Str.php(361): mb_strtolower(Array, 'UTF-8')
// etc.

This PR is a stupid-simple fix that just JSON-encodes non-scalar values on the theory that it will result in a string that Str::contains() will be able to inspect for the search term.

@macbookandrew
Copy link
Contributor Author

The biggest benefit I see is being able to use the Collection engine in tests that depend on Scout search results (i.e., testing the content of a page rendered by a search controller).

@taylorotwell taylorotwell merged commit ea568a6 into laravel:9.x Sep 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants