{"record":{"id":"7449463d26efeddc","repo":"mongodb/laravel-mongodb","slug":"first-argument-of-s-must-be-a-field-path-as-string-got-s","errorCode":null,"errorMessage":"First argument of %s must be a field path as \"string\". Got \"%s\"","messagePattern":"First argument of (.+?) must be a field path as \"string\"\\. Got \"(.+?)\"","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Query/Builder.php","lineNumber":1314,"sourceCode":"            if (is_string($operator) && str_starts_with($operator, '$')) {\n                $operator = substr($operator, 1);\n            }\n\n            if ($operator === self::UNSAFE_FIELD_QUERY) {\n                $operator = '=';\n            } elseif ($operator === '=' && self::firstOperatorKey($params[2]) !== null) {\n                $this->throwIfIdLikeOperatorValue($params[0], $params[2]);\n\n                $params[2] = ['$eq' => $params[2]];\n            }\n        }\n\n        if (func_num_args() === 1 && ! is_array($column) && ! is_callable($column)) {\n            throw new ArgumentCountError(sprintf('Too few arguments to function %s(%s), 1 passed and at least 2 expected when the 1st is not an array or a callable', __METHOD__, var_export($column, true)));\n        }\n\n        if (is_float($column) || is_bool($column) || $column === null) {\n            throw new InvalidArgumentException(sprintf('First argument of %s must be a field path as \"string\". Got \"%s\"', __METHOD__, get_debug_type($column)));\n        }\n\n        return parent::where(...$params);\n    }\n\n    /** Array-of-wheres calls are marked so their generated \"=\" keeps building an operator document. */\n    #[Override]\n    protected function addArrayOfWheres($column, $boolean, $method = 'where')\n    {\n        return $this->whereNested(function ($query) use ($column, $method, $boolean) {\n            foreach ($column as $key => $value) {\n                if (is_numeric($key) && is_array($value)) {\n                    $query->{$method}(...array_values($value), boolean: $boolean);\n                } else {\n                    $query->{$method}($key, self::UNSAFE_FIELD_QUERY, $value, $boolean);\n                }\n            }\n        }, $boolean);","sourceCodeStart":1296,"sourceCodeEnd":1332,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Query/Builder.php#L1296-L1332","documentation":"The first argument to where() must be a field path string (or an array/callable). Floats, booleans and null are rejected immediately, because Mongo field paths are strings and there is no sane interpretation of where(1.5, ...).","triggerScenarios":"->where(1.5, '>', 10); ->where(true); ->where(null, 'exists'); variables that were meant to hold a column name but contain a number/bool/null (e.g. from misordered list destructuring or JSON keys cast to ints).","commonSituations":"Dynamic column names sourced from config/input that came out non-string; typos passing a value where a column was expected.","solutions":["Pass the field path as a string: ->where('score', '>', 10).","Cast/coerce the column: ->where((string) $column, $op, $value) after verifying it is a valid path.","Verify upstream data so the column variable cannot be null/bool/float (add assertions).","Use the array form ->where([$column => $value]) for dynamic columns, ensuring keys are strings."],"exampleFix":"// before\n$col = 1; // came from array keys\n$query->where($col, '>', 5);\n// after\n$col = 'position';\n$query->where($col, '>', 5);","handlingStrategy":"type-guard","validationCode":"if (!is_string($column) || $column === '') {\n    throw new InvalidArgumentException('Field path must be a non-empty string');\n}","typeGuard":"function isFieldPath(mixed $v): bool {\n    return is_string($v) && $v !== '';\n}","tryCatchPattern":null,"preventionTips":["Keep column names in typed string constants/config.","Assert column variables are strings before where().","Avoid passing values where columns are expected."],"tags":["php","laravel-mongodb","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}