Skip to content

[12.x] Add docs about using 'exists' rule on array #10374

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

DeffiDeffi
Copy link

I recently stumbled upon this bit of (to my knowledge) undocumented behaviour of the exists rule.

I assumed that exists would not work on array values because the documentation did not specify that it would. So I validated incoming arrays of IDs like this:

'user_ids' => ['array'],
'user_ids.*' => ['integer', 'exists:users,id'],

This queries the database for every single item in the array. Instead, you can specify the 'exists' rule as:

'user_ids' => ['array', 'exists:users,id'],

... which only generates a single WHERE id IN (...) query. This PR adds a little note on this to the docs.

@shaedrich
Copy link
Contributor

Good catch 👍🏻

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