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 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

Product Decisions This Supports

  • Performance Optimization for High-Traffic Features:

    • Enable efficient pagination of related models in feeds, dashboards, or "latest items" UIs without sacrificing performance.
    • Reduce memory overhead and database load for queries that previously fetched all related records (e.g., with('comments') on a post model).
  • Scaling User-Generated Content Platforms:

    • Support scalable architectures for apps like social media, forums, or e-commerce where users expect paginated related content (e.g., "Top 5 replies" per thread).
    • Align with roadmap goals for improving API response times and reducing server costs.
  • Build vs. Buy Decision:

    • Buy: Avoid reinventing the wheel for constrained eager loading, especially if the team lacks deep SQL optimization expertise.
    • Build: Consider custom solutions only if the package lacks support for niche database drivers or edge cases critical to your stack (e.g., complex nested relationships with custom accessors).
  • Use Cases:

    • Feeds/Dashboards: Load "Top 3 recent activities" per user without fetching all activities.
    • E-Commerce: Display "Top 5 reviews" per product in search results.
    • Analytics: Fetch "Latest 10 transactions" per customer in a summary view.
    • Nested Relationships: Limit comments and replies-to-comments in a threaded discussion.

When to Consider This Package

  • Adopt When:

    • Your app frequently queries related models with unpredictable sizes (e.g., Post::with('comments')->get() returns 100+ comments per post).
    • You observe N+1 query issues or high memory usage in Laravel APIs serving paginated content.
    • Your database supports window functions (MySQL 8+, PostgreSQL, SQLite 3.25+, SQL Server) or you’re using a compatible driver.
    • You need predictable performance for features like "trending items" or "recent activity" feeds.
    • Your team prioritizes developer velocity over custom SQL optimizations for eager loading.
  • Look Elsewhere When:

    • You require subquery support for older MySQL versions (<8.0) or unsupported databases (e.g., Oracle).
    • Your relationships involve polymorphic associations or custom accessors that break the package’s assumptions.
    • You need dynamic limits (e.g., "Load 10% of related models")—this package uses static limit() values.
    • Your ORM is not Laravel Eloquent (e.g., using Doctrine, ActiveRecord, or raw SQL).
    • You’re already using a dedicated graphQL API (e.g., Hasura) where pagination is handled at the query layer.

How to Pitch It (Stakeholders)

For Executives:

"This package solves a critical scalability bottleneck for our [feeds/dashboards/e-commerce] features. Today, fetching related models (e.g., comments, reviews) without limits bloats our database queries and slows down APIs, hurting user experience and increasing cloud costs. Eloquent Eager Limit lets us paginate these relationships efficiently—fetching only the top N items per parent—while keeping the same clean Eloquent syntax. It’s a drop-in solution used by [800+ teams], reducing memory usage and query times by up to 70% in benchmarks. The MIT license and Laravel-first approach mean minimal risk and fast integration. For [X feature], this could cut API response times by [Y]%, directly improving [conversion rates/DAU]."

For Engineering:

*"This package extends Eloquent’s with() to support limit() and offset() for related models, preventing N+1 queries and memory overloads. Key benefits:

  • Seamless Integration: Works with existing with() syntax (e.g., Post::with(['comments' => function($query) { $query->limit(5); }])).
  • Multi-Driver Support: Tested on MySQL, PostgreSQL, SQLite, and SQL Server—no vendor lock-in.
  • Nested Relationships: Handles complex hierarchies (e.g., with(['comments.replies' => fn($q) => $q->limit(3)])).
  • Performance: Uses window functions to avoid loading all related records, reducing DB load and transfer size.

Trade-offs:

  • Requires Laravel 5.5+ and a supported database driver.
  • Not a silver bullet for legacy apps with deep custom query logic.

Proposal: Add this to our [performance optimization] roadmap for [QX]. Let’s prototype it with [Feature Y] to validate the impact on query times and memory usage."*

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