Add Configurable Scout Key Type #752
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
this PR improves the Scout integration when using custom keys for Searchable Models.
Problem
This problem was already addressed in the following issue:
#741
In one of our applications, we use incrementing IDs as the primary key for our models. Additionally, we are using the
HasUuids
trait to add a uuid column on top of the incrementing id. We did that because we didn't want to expose our incrementing IDs to the front end (and the search engine) due to security concerns.We have done that by modifying the `getScouteKey' method.
So far, so good indexing of these models works. But when we want to use the
Model::search()->get()
method within the query builder, we get a database exception because Scout thinks our primary key is an integer.This happens within the
queryScoutModelsByIds
method. The scout package determines the used key type (integer or string) via the EloquentgetKeyType()
method. This behavior is not correct in this case. When you can define a custom Scout key column, you should also be able to define the column type of this custom key.Workaround
Before this PR, The workaround for this problem is to override the
queryScoutModelsByIds()
method within the models so the builder is doing awhereIn()
instead of awhereIntegerRaw()
condition.Solution
I have added a new
getScoutKeyType(): string
method, which, by default, returns the model key type. This new method allows you to customize the key type Scout uses for querying models.This should not be a breaking change since it does not change the default behavior of Scout, but it adds a little more flexibility in how custom key types can be used in Scout. There is a minor risk of naming collisions, but since the new method is prefixed with the scout word, it should be no problem.