Skip to content

Fix/paginate ignores with eager load#62

Merged
benkhalife merged 2 commits into
mainfrom
fix/paginate-ignores-with-eager-load
Jun 30, 2026
Merged

Fix/paginate ignores with eager load#62
benkhalife merged 2 commits into
mainfrom
fix/paginate-ignores-with-eager-load

Conversation

@benkhalife

Copy link
Copy Markdown
Member

No description provided.

EagerBuilder only declared get() and first() as terminal methods that
trigger Model::eagerLoad(). paginate() fell through __call() straight
to the underlying Query\Builder::paginate(), which runs its own get()
internally and never touches eager loading — silently dropping any
with() chained before it.

Practical effect: Model::with('relation')->paginate(...) returned a
page whose rows had the relation unloaded. Accessing it afterwards
fell back to lazy loading per row, reproducing full N+1 (e.g. 12
queries for a 10-row page instead of 3) with no error to signal it.

Adds EagerBuilder::paginate(), which runs the page query then calls
Model::eagerLoad() on $page->data before returning — mirroring what
get() and first() already do.
Adds coverage for Model::with(...)->paginate(...) and the
select()->with()->paginate() chain, asserting the relation is marked
loaded on each row of the page. Also adds a query-count test proving
paginate() + with() now uses a constant 3 queries (count + page
select + one whereIn for the relation) on a 10-row page, instead of
3 + N from the silent fallback to lazy loading.
@benkhalife benkhalife merged commit 7e04d9c into main Jun 30, 2026
11 checks passed
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