[7.x] Fix regressions on find* methods with Arrayable ids #30312
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.
Ensure Builder::findOrFail with Arrayable throws ModelNotFoundException
I found a regression in #7048 and #19019, where the Eloquent Builder will not throw a
ModelNotFoundException
when anArrayable
is passed tofindOrFail
.In #7048, we are checking if the
$ids
is an array and we count the results against the number of ids.But since #19019, the
find
method also accepts anArrayable
for the ids.So if an
Arrayable
is passed, the check is skipped andfindOrFail
returns the results.To fix this, we are first checking if the
$ids
is anArrayable
and we convert the ids to an array before checking the results.Ensure find* methods on relationships are accepting Arrayable ids
This regression is also observed in #9143, because the
find
,findMany
andfindOrFail
methods were copied from the Eloquent Builder to theBelongsToMany
andHasManyThrough
relations, but they did not account for Arrayable ids.For this reason, we need to convert the passed ids to an array before executing the queries.
This is a potentially breaking change, so this should not be merged in 6.x. The reason is because in 6.x a collection/arrayable of ids will not throw an exception if the number of ids does not match the number of found records. While an array with the same ids would throw an exception:
For example: