{"id":"667a433c7256ad1c","repo":"laravel/framework","slug":"illegal-operator-and-value-combination","errorCode":null,"errorMessage":"Illegal operator and value combination.","messagePattern":"Illegal operator and value combination\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":1068,"sourceCode":"        }, $boolean);\n    }\n\n    /**\n     * Prepare the value and operator for a where clause.\n     *\n     * @param  string  $value\n     * @param  string  $operator\n     * @param  bool  $useDefault\n     * @return array\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function prepareValueAndOperator($value, $operator, $useDefault = false)\n    {\n        if ($useDefault) {\n            return [$operator, '='];\n        } elseif ($this->invalidOperatorAndValue($operator, $value)) {\n            throw new InvalidArgumentException('Illegal operator and value combination.');\n        }\n\n        return [$value, $operator];\n    }\n\n    /**\n     * Determine if the given operator and value combination is legal.\n     *\n     * Prevents using Null values with invalid operators.\n     *\n     * @param  string  $operator\n     * @param  mixed  $value\n     * @return bool\n     */\n    protected function invalidOperatorAndValue($operator, $value)\n    {\n        return is_null($value) && in_array($operator, $this->operators) &&\n             ! in_array($operator, ['=', '<=>', '<>', '!=']);","sourceCodeStart":1050,"sourceCodeEnd":1086,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L1050-L1086","documentation":"Thrown by prepareValueAndOperator when invalidOperatorAndValue is true: the value is null AND the operator is a known SQL operator that is NOT one of '=', '<=>', '<>', '!='. Laravel only allows null with the null-safe/equality operators; combining null with '>', '<', 'like', 'between', etc. is rejected because it produces semantically wrong SQL. Typically surfaces when a where clause receives an unset request input defaulting to null paired with a comparison operator.","triggerScenarios":"`->where('age', '>', $request->input('min_age'))` when min_age is absent (null). `->where('name', 'like', $maybeNull)`. Passing `null` as the value to `whereBetween`. Using an operator like '>' against a nullable column without first null-checking the bound value.","commonSituations":"Optional URL filters where the controller does `$q->where('price', '>=', $request->price)` without filtering nulls; data imports where a source field is empty and coerced to null; conditional where clauses built dynamically without null guards.","solutions":["Only apply the where clause when the value is non-null: `$request->filled('min_age') && $q->where('age', '>', $request->min_age)` inside a when() block.","Use a null-safe operator: `->whereNull('col')` for null checks, or `where('col', '=', null)`.","Coerce the value with a default: `$request->float('min_age', 0)`.","Use `when($request->has('x'), fn($q) => $q->where(...))` to skip clauses with missing input."],"exampleFix":"// before\n$query->where('price', '>', $request->input('min_price'));\n// when min_price is null => Illegal operator and value combination.\n\n// after\n$query->when($request->filled('min_price'),\n    fn ($q) => $q->where('price', '>', $request->input('min_price')));","handlingStrategy":"validation","validationCode":"// before building a where clause with a comparison operator\nif ($value === null && ! in_array($operator, ['=','<=>','<>','!='], true)) {\n    // skip the clause or switch to whereNull\n    return;\n}","typeGuard":"function isLegalNullOperator(string $operator): bool\n{\n    return in_array(strtolower($operator), ['=','<=>','<>','!='], true);\n}","tryCatchPattern":"// Prefer validation over try/catch here. If you catch, re-run with a sanitized operator:\ntry {\n    $q->where($col, $op, $val);\n} catch (\\InvalidArgumentException $e) {\n    if ($val === null) { $q->whereNull($col); }\n}","preventionTips":["Use `when($request->filled('x'), fn($q) => $q->where('x', $op, $request->x))` for optional filters.","Whitelist comparison operators against user-supplied direction params.","Validate request inputs with numeric/required rules so null never reaches a comparison clause."],"tags":["query-builder","where","null-handling","invalid-operator"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}