Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Eloquent Eager Limit

Eloquent Eager Limit Laravel Package

staudenmeir/eloquent-eager-limit

Add per-parent limits to eager-loaded Eloquent relationships in Laravel. Load only the latest N related models for each parent (e.g., newest comments per post) without N+1 queries. Supports common relations and integrates cleanly with queries and pagination.

View on GitHub
Deep Wiki
Context7

Laravel Eloquent eager loading with limit

Frequently asked questions about Eloquent Eager Limit
How do I limit eager-loaded relations to only the top N records per parent model in Laravel?
Use the `withLimit()` method to constrain eager-loaded relations. For example, replace `Post::with(['comments' => function($query) { $query->limit(5); }])` with `Post::withLimit(['comments' => 5])`. This ensures only the latest 5 comments are loaded per post without N+1 queries.
Does this package work with Laravel 10, 9, or older versions? What’s the minimum Laravel requirement?
The package is designed for Laravel 9+ and fully supports Laravel 10. It leverages Eloquent’s query builder features introduced in these versions, so older Laravel 8 or below may require adjustments or won’t work without compatibility layers.
Can I use `withLimit` with nested eager loading (e.g., `comments.user`)?
Yes, nested relations are supported. For example, `Post::withLimit(['comments.user' => 3])` will load only the top 3 users per comment per post. This works for deeply nested structures like forums or hierarchical data, but test performance with complex queries.
Will this package break existing eager loading queries in my Laravel app?
No, it’s a drop-in extension. Existing `with()` queries remain unchanged, and `withLimit()` is an optional addition. You can gradually migrate queries without disrupting functionality, making it safe for production use.
How does `withLimit` handle pagination conflicts (e.g., `Post::paginate(10)->withLimit(['comments' => 2])`)?
The package prioritizes relation limits over pagination. If you paginate parents and limit relations, only the top N relations for each paginated parent will load. For consistent results, paginate parents separately and limit relations in a second query if needed.
Does this work with PostgreSQL’s `DISTINCT ON` or other database-specific features?
The package supports PostgreSQL but may require manual adjustments for advanced features like `DISTINCT ON`. For simple ordering (e.g., `ORDER BY created_at DESC`), it works out of the box. Check the issue tracker for PostgreSQL-specific edge cases or test thoroughly in your environment.
How can I cache eager-loaded relations with limits to avoid repeated database queries?
Use Laravel’s cache tags or implement custom caching logic. For example, cache the parent model with a tag like `posts-with-comments` and invalidate it when comments change. Avoid caching `withLimit` results globally unless you handle cache key collisions manually.
Are there performance risks with deeply nested `withLimit` queries (e.g., 4+ levels deep)?
Yes, excessive nesting can increase query complexity and slow performance. Benchmark with realistic data volumes and avoid deeply nested structures in high-traffic applications. For complex hierarchies, consider lazy-loading or flattening relations.
What alternatives exist for limiting eager-loaded relations in Laravel?
Alternatives include manually adding `limit()` in closure callbacks (e.g., `with(['comments' => fn($q) => $q->limit(5)])`) or using raw SQL with `join` and `whereIn`. However, these require more boilerplate and don’t handle nested relations as cleanly as `withLimit`.
How do I handle SQL Server’s `TOP` syntax instead of `LIMIT` in this package?
The package abstracts `LIMIT`/`OFFSET` syntax and should work with SQL Server out of the box. If you encounter issues, check for database-specific query builder overrides in Laravel or consult the package’s issue tracker for SQL Server compatibility notes.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle