mongodb/laravel-mongodb · error · BadMethodCallException

This method is not supported by MongoDB. Try "toMql()"…

Error message

This method is not supported by MongoDB. Try "toMql()" instead.

What it means

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).

Solutions

  1. Replace toSql() with toMql() to inspect the MongoDB query the builder will execute
  2. Remove or gate the toSql() call behind a driver check (e.g. only call it when the connection driver is not mongodb)
  3. Replace the dump with ->getQuery() or ->toMql() in debugging/telescope/log code
  4. If a package calls toSql() internally, use a MongoDB-aware fork or wrap the call in a method_exists/driver check

Example fix

// before
$query = DB::table('users')->where('age', '>', 18);
echo $query->toSql();

// after
$query = DB::table('users')->where('age', '>', 18);
echo json_encode($query->toMql());
Defensive patterns

Strategy: try-catch

Validate before calling

if (method_exists($builder, 'toMql')) { $repr = $builder->toMql(); }

Type guard

$isMongo = $builder instanceof MongoDB\Laravel\Query\Builder;

Try / catch

try { $sql = $builder->toSql(); } catch (\BadMethodCallException $e) { $repr = $builder->toMql(); }

Prevention

When it happens

Trigger: Calling ->toSql() (or dd()->toSql(), DB::table(...)->toSql(), or debugging helpers that dump the SQL) on a MongoDB query builder instance.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of mongodb/laravel-mongodb@0634653039 (2026-09-15). Data as JSON: /api/errors/4d271d98f883ad30. Report an issue: GitHub.

Appendix: source

Thrown at src/Query/Builder.php:1911

        return $options;
    }

    /** @inheritdoc */
    #[Override]
    public function __call($method, $parameters)
    {
        if ($method === 'unset') {
            return $this->drop(...$parameters);
        }

        return parent::__call($method, $parameters);
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function toSql()
    {
        throw new BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function toRawSql()
    {
        throw new BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
    {
        throw new BadMethodCallException('This method is not supported by MongoDB');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]

View on GitHub (pinned to 0634653039)