Skip to content

Honor overridden query() on instance calls to the model - #380

Merged
techmahedy merged 2 commits into
doppar:4.xfrom
techmahedy:techmahedy-4.x
Oct 19, 2026
Merged

techmahedy merged 2 commits into
doppar:4.xfrom
techmahedy:techmahedy-4.x

Conversation

@techmahedy

@techmahedy techmahedy commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Pull Request Checklist

Q A
Branch? 4.x
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

Description

Overriding query() on a model only took effect for static calls such as User::all(), User::first() and User::paginate(). Instance calls such as $user->first() bypassed it. This is what happens when a model is injected into a controller method.

#[Override]
public static function query(?string $connection = null): Builder
{
    return parent::query($connection)->where('id', '>', 2);
}

Cause: Model::__call() built its builder with $this->newQuery(), which creates a new Builder(...) directly. The static query() was never called, so the override was skipped.

Fix: Model::__call() now uses static::query($this->connection), so static and instance calls share one entry point.

return static::query($this->connection)->$method(...$parameters);

newQuery() is unchanged. Internal persistence (save, update, delete by key) still uses it, so a query() override does not leak into writes.

Behavior notes

  • Dynamic instance calls now respect an overridden query().
  • query() builds a fresh new static(), so runtime changes made on the instance are ignored by dynamic calls. Examples are $model->setTable('other') followed by $model->where(...), or a per-instance $pageSize. Class-level $table and $pageSize behave as before.

Testing

  • Manually verified that $user->first() in an injected controller method now applies the query() override.

Checklist

  • Tests have been added or updated
  • Documentation has been updated if necessary
  • Code follows the project coding standards
  • All tests pass locally

@techmahedy techmahedy added the bug Something isn't working label Oct 19, 2026
@techmahedy
techmahedy merged commit 8e6a28b into doppar:4.x Oct 19, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant