laravel/framework · error · InvalidArgumentException
Illegal operator and value combination.
Error message
Illegal operator and value combination.
What it means
prepareValueAndError/prepareValueAndOperator validates the operator+value pair for a where clause. invalidOperatorAndValue() returns true when the value is null but the operator is not one of the null-safe ones ('=', '<>', '!=', '<=>'); combining a null value with operators like '>', '<', 'like' is ambiguous SQL and is rejected. The two-argument form (where('col', 'value')) sets useDefault and is exempt.
Solutions
- When the value can be null, branch to whereNull()/whereNotNull() instead of using a comparison operator.
- Default or coerce the incoming value before passing it to where() (e.g. fall back to 0 or '').
- Use the two-argument form ->where('col', $value) when you want an equality check, and let Laravel treat null correctly.
Example fix
// before
->where('published_at', '>', $request->since) // $request->since is null
// after
->when($request->since, fn ($q) => $q->where('published_at', '>', $request->since)) Defensive patterns
Strategy: validation
Validate before calling
$operator = '>';
if ($value === null && ! in_array($operator, ['=', '<>', '!=', '<=>'], true)) {
// route to whereNull/whereNotNull instead
$query->whereNull('column');
} else {
$query->where('column', $operator, $value);
} Prevention
- Branch on null inputs and use whereNull()/whereNotNull() rather than a comparison operator.
- Use when()/unless() to apply where() only when a filter value is present.
- Validate/cast request input before it reaches the query layer.
When it happens
Trigger: ->where('column', '>', null), ->where('price', '<', null), ->where('name', 'like', null) — any where() with an explicit comparison operator and a null value that the operator cannot meaningfully apply to.
Common situations: A variable that should hold a value but came back null from input/request data; nullable columns compared with comparison operators instead of whereNull(); API clients sending null for a filter; building dynamic where clauses without null-checking the input.
Related errors
- A subquery must be a query builder instance, a Closure, or…
- $count records were found.
- Invalid binding type
- Nested arrays may not be passed to whereIn method.
- No record found for the given query.
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/667a433c7256ad1c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Database/Query/Builder.php:1068
}, $boolean);
}
/**
* Prepare the value and operator for a where clause.
*
* @param string $value
* @param string $operator
* @param bool $useDefault
* @return array
*
* @throws \InvalidArgumentException
*/
public function prepareValueAndOperator($value, $operator, $useDefault = false)
{
if ($useDefault) {
return [$operator, '='];
} elseif ($this->invalidOperatorAndValue($operator, $value)) {
throw new InvalidArgumentException('Illegal operator and value combination.');
}
return [$value, $operator];
}
/**
* Determine if the given operator and value combination is legal.
*
* Prevents using Null values with invalid operators.
*
* @param string $operator
* @param mixed $value
* @return bool
*/
protected function invalidOperatorAndValue($operator, $value)
{
return is_null($value) && in_array($operator, $this->operators) &&
! in_array($operator, ['=', '<=>', '<>', '!=']);View on GitHub (pinned to e0f6eb3518)