{"id":"8dc54a35e6cc07ac","repo":"laravel/framework","slug":"nested-arrays-may-not-be-passed-to-wherein-method","errorCode":null,"errorMessage":"Nested arrays may not be passed to whereIn method.","messagePattern":"Nested arrays may not be passed to whereIn method\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":1447,"sourceCode":"        if ($this->isQueryable($values)) {\n            [$query, $bindings] = $this->createSub($values);\n\n            $values = [new Expression($query)];\n\n            $this->addBinding($bindings, 'where');\n        }\n\n        // Next, if the value is Arrayable we need to cast it to its raw array form so we\n        // have the underlying array value instead of an Arrayable object which is not\n        // able to be added as a binding, etc. We will then add to the wheres array.\n        if ($values instanceof Arrayable) {\n            $values = $values->toArray();\n        }\n\n        $this->wheres[] = ['type' => $type, 'column' => $column, 'values' => $values, 'boolean' => $boolean];\n\n        if (count($values) !== count(Arr::flatten($values, 1))) {\n            throw new InvalidArgumentException('Nested arrays may not be passed to whereIn method.');\n        }\n\n        // Finally, we'll add a binding for each value unless that value is an expression\n        // in which case we will just skip over it since it will be the query as a raw\n        // string and not as a parameterized place-holder to be replaced by the PDO.\n        $this->addBinding($this->cleanBindings($values), 'where');\n\n        return $this;\n    }\n\n    /**\n     * Add an \"or where in\" clause to the query.\n     *\n     * @param  \\Illuminate\\Contracts\\Database\\Query\\Expression|string  $column\n     * @param  mixed  $values\n     * @return $this\n     */\n    public function orWhereIn($column, $values)","sourceCodeStart":1429,"sourceCodeEnd":1465,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L1429-L1465","documentation":"Thrown by whereIn after the values are stored: if count($values) !== count(Arr::flatten($values, 1)) the array contains nested arrays, which whereIn cannot represent as a flat IN-list. Laravel flattens one level for collections of Expression objects, but genuine multi-dimensional arrays (arrays of arrays of scalars) are rejected because they would produce malformed bindings or an unusable IN clause.","triggerScenarios":"`whereIn('id', [[1,2],[3,4]])`. Passing a grouped result like `User::all()->groupBy('team')->map->pluck('id')->toArray()`. Accidentally wrapping a Collection in an array `[collect([1,2])]`. Passing the result of a `->toArray()` on a nested relationship payload.","commonSituations":"Pre-grouping IDs by category then feeding the grouped structure directly into whereIn; converting Eloquent collections of collections to arrays; off-by-one in a reduce/map that leaves an extra array layer.","solutions":["Flatten the array before passing: `whereIn('id', Arr::flatten($nested, 1))`.","Use the inner collection directly: `whereIn('id', $group->pluck('id'))` instead of the outer structure.","If you need OR-over-groups, build multiple orWhereIn clauses in a loop.","Inspect with `dd(Arr::flatten($values))` to confirm the expected flat list."],"exampleFix":"// before\n$teams = User::all()->groupBy('team')->map->pluck('id')->toArray();\nUser::whereIn('id', $teams)->get();\n// => Nested arrays may not be passed to whereIn method.\n\n// after\n$ids = \\Illuminate\\Support\\Arr::flatten($teams, 1);\nUser::whereIn('id', $ids)->get();","handlingStrategy":"validation","validationCode":"$flat = \\Illuminate\\Support\\Arr::flatten($values, 1);\nif (count($values) !== count($flat)) {\n    throw new InvalidArgumentException('whereIn values contain nested arrays; flattening required.');\n}\n$query->whereIn($col, $flat);","typeGuard":"function isFlatArray(array $arr): bool\n{\n    return count($arr) === count(\\Illuminate\\Support\\Arr::flatten($arr, 1));\n}","tryCatchPattern":"// Validation is preferable. Catch only if the source shape is opaque:\ntry {\n    $q->whereIn('id', $ids);\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'Nested arrays')) {\n        $q->whereIn('id', \\Illuminate\\Support\\Arr::flatten($ids, 1));\n    }\n}","preventionTips":["Always pluck a single level: `->pluck('id')->all()` before whereIn.","Run `Arr::flatten` on grouped/collection-derived inputs.","Add a unit test asserting flat inputs to whereIn."],"tags":["query-builder","where-in","array-shape","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}