Skip to content

Document associative array where clauses #10583

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

Conversation

MarkHolmesRex
Copy link
Contributor

In January 2016 a feature was added to allow where clauses with associative arrays, where the column name is the array key and the where value is the array value. It seems like this was never documented, however I imagine since it's such an intuitive way to use the where clause a number of projects are likely using it without realising it's an undocumented feature. It's also a good feature that should stay as it's a common use case to already have such an array and want to be able to use it in a query.

Code block (the else statement is the feature implementation):

    protected function addArrayOfWheres($column, $boolean, $method = 'where')
    {
        return $this->whereNested(function ($query) use ($column, $method, $boolean) {
            foreach ($column as $key => $value) {
                if (is_numeric($key) && is_array($value)) {
                    $query->{$method}(...array_values($value), boolean: $boolean);
                } else {
                    $query->{$method}($key, '=', $value, $boolean);
                }
            }
        }, $boolean);
    }

This code is still there in the latest version.

@taylorotwell taylorotwell merged commit c205dfd into laravel:12.x Jul 8, 2025
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