Skip to content

Conversation

@binaryfire
Copy link
Contributor

@binaryfire binaryfire commented Jan 18, 2026

Summary

Model, Pivot, and MorphPivot now delegate to $connection->query() when creating base query builders, instead of hardcoding new QueryBuilder(...).

This enables custom connections to provide custom query builders with additional methods without requiring macros or upstream PRs.

Laravel does it this way as well.

Changes

Override newBaseQueryBuilder() in:

  • Hypervel\Database\Eloquent\Model
  • Hypervel\Database\Eloquent\Relations\Pivot
  • Hypervel\Database\Eloquent\Relations\MorphPivot
protected function newBaseQueryBuilder()
{
    /** @var \Hyperf\Database\Connection $connection */
    $connection = $this->getConnection();

    return $connection->query();
}

Usage

// Custom connection with custom query builder
class PostgresConnection extends BasePostgresConnection
{
    public function query(): Builder
    {
        return new CustomBuilder($this, $this->getQueryGrammar(), $this->getPostProcessor());
    }
}

// Register it
Connection::resolverFor('pgsql', fn (...$args) => new PostgresConnection(...$args));

// Now all models use the custom builder
User::query()->whereSomeCustomMethod('xxxxx')->get();

Backwards Compatibility

No breaking changes. Connection::query() already does the same instantiation - now we just delegate to it instead of duplicating.

Tests

Added NewBaseQueryBuilderTest with 3 tests verifying Model, Pivot, and MorphPivot all delegate to the connection's query() method.

Model, Pivot, and MorphPivot now delegate to $connection->query() when
creating base query builders, enabling custom connections to provide
custom query builders with additional methods.
@binaryfire binaryfire changed the title feat: Allow custom query builders via connection feat: allow custom query builders via connection Jan 18, 2026
@binaryfire
Copy link
Contributor Author

Closing. Will be part of a bigger database PR

@binaryfire binaryfire closed this Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant