{"record":{"id":"4d271d98f883ad30","repo":"mongodb/laravel-mongodb","slug":"this-method-is-not-supported-by-mongodb-try-tomql-instead","errorCode":null,"errorMessage":"This method is not supported by MongoDB. Try \"toMql()\" instead.","messagePattern":"This method is not supported by MongoDB\\. Try \"toMql\\(\\)\" instead\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/Query/Builder.php","lineNumber":1911,"sourceCode":"        return $options;\n    }\n\n    /** @inheritdoc */\n    #[Override]\n    public function __call($method, $parameters)\n    {\n        if ($method === 'unset') {\n            return $this->drop(...$parameters);\n        }\n\n        return parent::__call($method, $parameters);\n    }\n\n    /** @internal This method is not supported by MongoDB. */\n    #[Override]\n    public function toSql()\n    {\n        throw new BadMethodCallException('This method is not supported by MongoDB. Try \"toMql()\" instead.');\n    }\n\n    /** @internal This method is not supported by MongoDB. */\n    #[Override]\n    public function toRawSql()\n    {\n        throw new BadMethodCallException('This method is not supported by MongoDB. Try \"toMql()\" instead.');\n    }\n\n    /** @internal This method is not supported by MongoDB. */\n    #[Override]\n    public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')\n    {\n        throw new BadMethodCallException('This method is not supported by MongoDB');\n    }\n\n    /** @internal This method is not supported by MongoDB. */\n    #[Override]","sourceCodeStart":1893,"sourceCodeEnd":1929,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Query/Builder.php#L1893-L1929","documentation":"Laravel's Eloquent/Query Builder API includes toSql(), which renders the query as a SQL string. mongodb/laravel-mongodb overrides it in src/Query/Builder.php and throws a BadMethodCallException because MongoDB queries are not SQL. The library suggests toMql() instead, which returns the MongoDB query representation (MQL pipeline/filter).","triggerScenarios":"Calling ->toSql() (or dd()->toSql(), DB::table(...)->toSql(), or debugging helpers that dump the SQL) on a MongoDB query builder instance.","commonSituations":"Copy-pasting SQL-debugging idioms from Laravel docs/tutorials into a MongoDB-backed app; IDE autocomplete offering toSql() since the builder extends Laravel's; switching a project from MySQL to MongoDB without updating debug code; writing generic query-logging middleware that assumes SQL.","solutions":["Replace toSql() with toMql() to inspect the MongoDB query the builder will execute","Remove or gate the toSql() call behind a driver check (e.g. only call it when the connection driver is not mongodb)","Replace the dump with ->getQuery() or ->toMql() in debugging/telescope/log code","If a package calls toSql() internally, use a MongoDB-aware fork or wrap the call in a method_exists/driver check"],"exampleFix":"// before\n$query = DB::table('users')->where('age', '>', 18);\necho $query->toSql();\n\n// after\n$query = DB::table('users')->where('age', '>', 18);\necho json_encode($query->toMql());","handlingStrategy":"try-catch","validationCode":"if (method_exists($builder, 'toMql')) { $repr = $builder->toMql(); }","typeGuard":"$isMongo = $builder instanceof MongoDB\\Laravel\\Query\\Builder;","tryCatchPattern":"try { $sql = $builder->toSql(); } catch (\\BadMethodCallException $e) { $repr = $builder->toMql(); }","preventionTips":["Never call SQL-specific Builder methods on MongoDB connections","Use toMql() for query inspection/debugging","Gate query-dumping code on the connection driver name","Search the codebase for toSql/toRawSql when migrating to MongoDB"],"tags":["mongodb","laravel","sql","unsupported-method"],"backgroundTag":"unsupported-operation","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"}