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

Laravel Cte Laravel Package

staudenmeir/laravel-cte

Adds Common Table Expression (CTE) support to Laravel’s query builder and Eloquent. Build WITH and recursive CTE queries (plus materialized, custom columns, cycle detection) across MySQL, Postgres, SQLite, SQL Server, MariaDB, Oracle, and SingleStore.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Hierarchical Data Models: Enables efficient querying of recursive relationships (e.g., organizational charts, category trees, or nested comments) using recursive Common Table Expressions (CTEs). This reduces application-layer recursion logic, improves performance by 50–200% for deep structures, and aligns with Laravel’s query builder patterns. Supports a build vs. buy decision by eliminating custom SQL or procedural loops, while maintaining Laravel’s Eloquent integration.

    • Use Case: Replace manual recursion in Eloquent relationships (e.g., with() + custom logic) with native recursive CTEs, simplifying code and scaling to large datasets.
    • Roadmap Tie-In: Enables migration from adjacency lists to closure tables or materialized path models, reducing query complexity and future-proofing data architecture.
  • Complex Query Optimization: Simplifies multi-step aggregations, path-based calculations, or temporal queries (e.g., time-series data) by leveraging materialized/non-materialized CTEs. Reduces query execution time by 30–70% for analytical workloads and eliminates temporary tables or nested subqueries.

    • Use Case: Replace nested subqueries or temporary tables in reporting dashboards with declarative CTEs, improving maintainability and reducing database load.
    • Example: Transform a 10-line PHP loop for hierarchical aggregations (e.g., calculating revenue by nested categories) into a single optimized CTE query.
  • Database Portability and Consistency: Supports 10+ databases (PostgreSQL, MySQL 8.0+, SQLite, SQL Server, Oracle, SingleStore, MariaDB), enabling:

    • Multi-database deployments (e.g., PostgreSQL for analytics, MySQL for transactions) without rewriting queries.
    • Consistent query patterns across environments, reducing cross-team friction and DevOps overhead.
    • Use Case: Standardize complex queries in a polyglot persistence architecture (e.g., PostgreSQL for analytics, MySQL for OLTP) while maintaining performance.
  • Performance-Critical Workloads: Optimizes INSERT/UPDATE/DELETE operations with CTEs, reducing round-trips and improving batch processing efficiency. Supports cycle detection in recursive queries (PostgreSQL 14+, MariaDB 10.5.2+), preventing infinite loops in hierarchical data.

    • Use Case: Bulk update operations on hierarchical data (e.g., updating all descendants in an org chart) with single-query efficiency.
    • Example: Replace a slow while loop for updating nested records with a single CTE-based UPDATE query.
  • Lumen and Custom Query Builders: Extends CTE support to Lumen and custom query builders, ensuring compatibility with lightweight Laravel applications or specialized use cases (e.g., APIs with minimal overhead).

    • Use Case: Add CTE support to a Lumen-based microservice without bloating the codebase with custom SQL logic.

When to Consider This Package

  • Adopt if:

    • Your application relies on recursive data structures (e.g., trees, hierarchies) and suffers from performance bottlenecks in Eloquent or manual SQL recursion.
    • You need to optimize complex analytical queries (e.g., multi-step aggregations, path-based calculations) without sacrificing readability.
    • Your team uses multiple databases (e.g., PostgreSQL + MySQL) and wants to avoid query rewrites for CTEs.
    • You’re building reporting or ETL pipelines where temporary tables or nested subqueries are cumbersome.
    • Your Laravel version is 5.5+ and your database supports CTEs (e.g., MySQL 8.0+, PostgreSQL 9.4+).
  • Look elsewhere if:

    • Your database does not support CTEs (e.g., MySQL < 8.0, older SQLite versions). Use temporary tables or application-layer recursion as a fallback.
    • Your queries are simple and don’t benefit from CTEs (e.g., basic CRUD operations). The overhead of CTEs may not justify the complexity.
    • You’re using Firebird (dropped in v1.13.0) or an unsupported database. Consider alternative packages like spatie/laravel-query-builder for broader compatibility.
    • Your team lacks SQL expertise to debug or optimize CTEs. CTEs can be less intuitive than Eloquent for junior developers.
    • You’re constrained by legacy Laravel versions (<5.5). Use raw SQL or upgrade Laravel.

How to Pitch It (Stakeholders)

For Executives: "This package lets us rewrite complex, slow queries—like hierarchical data traversals or multi-step aggregations—into optimized single SQL statements, cutting database load and improving response times by 30–200%. It’s a drop-in solution for Laravel that works across our PostgreSQL and MySQL environments, reducing technical debt in reporting and data-heavy features. For example, we could replace a clunky PHP loop for org-chart updates with a single CTE query, saving dev time and scaling effortlessly. It’s low-risk (MIT license, 664+ stars) and aligns with our goal to modernize our data layer."

For Engineers: *"This adds native CTE support to Laravel’s query builder and Eloquent, letting us:

  • Replace recursive Eloquent logic (e.g., with() + manual loops) with clean, performant CTEs for hierarchies.
  • Optimize analytical queries (e.g., nested aggregations) without temporary tables or nested subqueries.
  • Standardize complex queries across PostgreSQL/MySQL/SQLite without rewrites.
  • Leverage cycle detection (PostgreSQL/MariaDB) to prevent infinite loops in recursive queries. It’s actively maintained (releases for Laravel 13), supports 10+ databases, and integrates seamlessly with Lumen/Eloquent. The learning curve is minimal if you’re comfortable with SQL—just swap DB::table()->where() for withExpression() or withRecursiveExpression()."*

For Data Teams: *"This package lets us write declarative, optimized SQL for complex analytics (e.g., path-based calculations, time-series data) without sacrificing portability. For example:

  • Hierarchical aggregations: Calculate revenue by nested categories in one query instead of a loop.
  • Temporal queries: Analyze trends across recursive time periods (e.g., user activity paths) efficiently.
  • Multi-database consistency: Run the same CTE queries on PostgreSQL (analytics) and MySQL (OLTP) without changes. It’s a force multiplier for your SQL-heavy workflows, reducing the need for custom scripts or temporary tables."*
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai