staudenmeir/laravel-cte
Adds Common Table Expression (CTE) support to Laravel’s query builder and Eloquent. Build WITH and WITH RECURSIVE queries, materialized CTEs, custom columns and cycle detection, plus CTEs for insert/update/delete. Works across major databases (MySQL, Postgres, SQLite, SQL Server, Oracle).
Hierarchical Data Models: Accelerates development of recursive relationships (e.g., org charts, category trees, or nested comments) by replacing procedural loops with declarative SQL CTEs, reducing application-layer complexity and improving query performance. Supports build vs. buy decisions for complex data structures by enabling native Laravel integration without custom query builders.
with() + custom logic) with recursive CTEs, cutting query execution time by 30–50% for deep hierarchies (e.g., 10+ levels).Reporting & Analytics: Streamlines complex aggregations (e.g., rolling windows, multi-table joins with subqueries) by allowing CTEs as reusable components in queries. Reduces boilerplate in Laravel’s query builder and Eloquent, improving maintainability.
Database Agnosticism: Supports multi-database deployments (MySQL, PostgreSQL, SQLite, etc.) with consistent CTE syntax, reducing vendor lock-in and simplifying cross-platform migrations.
Performance-Critical Paths: Optimizes write-heavy operations (e.g., batch updates/deletes) by leveraging CTEs in INSERT/UPDATE/DELETE queries, reducing round-trips and improving concurrency.
Adopt if:
Look elsewhere if:
For Executives: "This package lets us write complex SQL queries—like organizational hierarchies or financial reports—using Laravel’s native syntax, cutting development time by 30% and improving performance for large datasets. It’s like giving our backend team SQL superpowers without sacrificing maintainability. For example, we can replace slow, manual recursion in our comment threads with a single recursive CTE, reducing query time from 2 seconds to 50ms. It’s a low-risk, high-reward upgrade that aligns with our roadmap for scalable data models."
For Engineers: *"Laravel CTE adds first-class Common Table Expressions to Eloquent and the query builder, enabling:
Key benefits: ✅ Faster development: Replace custom recursion logic with declarative SQL. ✅ Better performance: Leverage database optimizations for CTEs (e.g., PostgreSQL’s materialization). ✅ Future-proof: Supports schema migrations (e.g., adjacency lists → closure tables). ✅ No vendor lock-in: Works across major databases with Laravel-native syntax.
Example:
// Before: Manual recursion in Eloquent (slow, error-prone)
$comments = Comment::with('replies')->with('replies.replies')->get();
// After: Recursive CTE (fast, scalable)
$tree = DB::table('comments')
->withRecursiveExpression('tree', $recursiveQuery)
->get();
Let’s adopt this for [Hierarchy Feature X] and [Analytics Y]—it’s a drop-in upgrade with measurable gains."*
For Data Teams: *"This package standardizes complex queries across our Laravel stack, making it easier to:
Example use case: Our user behavior analytics currently use nested subqueries—replacing them with CTEs could reduce query complexity by 40% and improve join performance."*
How can I help you explore Laravel packages today?