Skip to content

Laravel v12 breaking change - Query Grammar requires a connection to be passed to constructor #42

@gdesoto

Description

@gdesoto

It looks like a breaking change was implemented in Laravel v12 in this PR: laravel/framework#54487

Any Query Grammar that extends Illuminate\Database\Grammar is now required to pass a connection when instantiated.

In my case I'm using the odbc drivers and the following connection options:

$options = [
  'processor' => \Illuminate\Database\Query\Processors\SqlServerProcessor::class,
  'grammar' => [
      'query' => \Illuminate\Database\Query\Grammars\SqlServerGrammar::class,
      'schema' => \Illuminate\Database\Schema\Grammars\SqlServerGrammar::class,
  ],
]

When trying to run a query, this error it thrown:

Too few arguments to function Illuminate\Database\Grammar::__construct(), 0 passed in vendor\yoramdelangen\laravel-pdo-odbc\src\ODBCConnection.php on line 15 and exactly 1 expected

The issue references the getDefaultQueryGrammar method in ODBCConnection.php

public function getDefaultQueryGrammar()
{
$queryGrammar = $this->getConfig('options.grammar.query');
if ($queryGrammar) {
return new $queryGrammar();
}
return parent::getDefaultQueryGrammar();
}

Updating the method to pass $this in line 15 resolved the issue for me.

    public function getDefaultQueryGrammar()
    {
        $queryGrammar = $this->getConfig('options.grammar.query');

        if ($queryGrammar) {
            return new $queryGrammar($this);
        }

        return parent::getDefaultQueryGrammar();
    }

Based on this comment laravel/framework#54487 (comment) the change wouldn't be backwards compatible. Perhaps, something like this is required:

        if ($queryGrammar) {
            return version_compare(app()->version(), '12.0.0', '<')
                ? new $queryGrammar()
                : new $queryGrammar($this);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions