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

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strong alignment with Laravel’s query builder/Eloquent: The package extends Laravel’s native query capabilities without breaking existing patterns, making it a drop-in enhancement for complex SQL use cases.
  • CTE-first design: Ideal for hierarchical data (e.g., org charts, category trees), recursive algorithms (e.g., pathfinding), and multi-stage aggregations (e.g., reporting). Reduces application-layer logic for SQL-heavy workflows.
  • Database-agnostic abstraction: Supports 10+ databases (MySQL 8+, PostgreSQL, SQLite, Oracle, etc.), but feature parity varies (e.g., cycle detection only in MariaDB 10.5.2+/PostgreSQL 14+). Requires database-specific feature awareness in implementation.
  • Composability: CTEs can be nested, recursive, or materialized, enabling modular query design (e.g., reusable subqueries for analytics or ETL pipelines).

Integration Feasibility

  • Low-risk for Laravel 5.5+: Minimal boilerplate (e.g., composer require, optional trait for Eloquent). No schema changes required.
  • Eloquent compatibility: Works with standard queries and recursive relationships (paired with laravel-adjacency-list for hierarchical models).
  • Lumen support: Requires manual query builder instantiation, adding minor complexity but maintaining functionality.
  • Oracle/SingleStore: Needs custom builder initialization, which may require connection-specific configuration.

Technical Risk

Risk Area Severity Mitigation
Database feature gaps High Validate target DB supports CTEs (e.g., MySQL 8+). Test cycle detection if using MariaDB/PostgreSQL.
Performance overhead Medium Materialized CTEs may increase memory usage; benchmark recursive queries.
Query complexity Medium CTEs can obscure SQL for junior devs; document usage patterns.
Lumen/Oracle edge cases Low Follow package docs for manual builder setup; test thoroughly.
PHP 8.4+ compatibility Low Package defaults to PHP 8.4; ensure CI/CD covers target PHP versions.

Key Questions for the TPM

  1. Database Strategy:
    • Which databases are in scope? Are cycle detection or materialized CTEs required?
    • How will query performance be monitored for recursive CTEs (e.g., depth > 100)?
  2. Adoption Impact:
    • Will this replace custom recursive Eloquent logic (e.g., with() + manual loops) or augment it?
    • Are there legacy queries that could benefit from CTE refactoring?
  3. Team Readiness:
    • Does the team have SQL expertise to debug complex CTEs, or will this require additional training?
    • How will query debugging (e.g., toSql()) be handled for CTE-heavy applications?
  4. Testing:
    • Should integration tests cover CTE edge cases (e.g., empty results, cycles)?
    • How will database-specific behaviors (e.g., PostgreSQL vs. MySQL) be tested?
  5. Future-Proofing:
    • Could this enable migration from adjacency lists to closure tables?
    • Are there plans to extend CTE support to Laravel’s query cache or scopes?

Integration Approach

Stack Fit

  • Laravel Core: Seamless integration with query builder and Eloquent (Laravel 5.5–13.x).
  • Database Drivers: Supports MySQL, PostgreSQL, SQLite, Oracle, SingleStore, etc., but feature parity varies (e.g., cycle detection).
  • PHP Version: Requires PHP 8.1+ (package defaults to 8.4); ensure compatibility with your stack.
  • Lumen: Requires manual builder instantiation (minor overhead).
  • Third-Party Packages: Works alongside laravel-adjacency-list for hierarchical models.

Migration Path

  1. Assessment Phase:
    • Audit existing recursive queries (e.g., hierarchical data, pathfinding) to identify CTE candidates.
    • Validate database support for target features (e.g., cycle detection).
  2. Pilot Implementation:
    • Start with non-recursive CTEs (e.g., withExpression() for subqueries).
    • Test materialized CTEs if using PostgreSQL/SQLite.
  3. Recursive Adoption:
    • Replace custom recursive Eloquent logic with withRecursiveExpression().
    • Pair with laravel-adjacency-list for hierarchical models.
  4. Full Rollout:
    • Extend to INSERT/UPDATE/DELETE queries with CTEs.
    • Document database-specific behaviors (e.g., Oracle syntax).

Compatibility

Component Compatibility Notes
Laravel 5.5–13.x ✅ Full support Version-specific packages available.
Eloquent ✅ With QueriesExpressions trait (L5.5–5.7) or auto-included (L5.8+)
Lumen ✅ Manual builder setup required Follow package docs.
Oracle/SingleStore ✅ Custom builder required Test thoroughly.
PHP 8.1+ ✅ Required (package defaults to 8.4) Adjust composer.json constraints if needed.
MySQL < 8.0 ❌ Unsupported Requires upgrade.
MariaDB < 10.2 ❌ Limited CTE support Cycle detection requires 10.5.2+.

Sequencing

  1. Phase 1: SELECT Queries
    • Replace simple subqueries with withExpression().
    • Test with non-recursive CTEs (e.g., reporting queries).
  2. Phase 2: Recursive Patterns
    • Migrate custom recursive logic (e.g., with() + loops) to withRecursiveExpression().
    • Validate cycle detection if using supported databases.
  3. Phase 3: DML Operations
    • Adopt CTEs in INSERT/UPDATE/DELETE queries.
  4. Phase 4: Eloquent Integration
    • Extend to Eloquent models (e.g., recursive relationships).
    • Document performance implications for deep hierarchies.

Operational Impact

Maintenance

  • Low Ongoing Effort:
    • No schema changes or migrations required.
    • Automatic updates via Composer (MIT license).
  • Dependency Risks:
    • Linked to Laravel version (e.g., v1.13 for Laravel 13).
    • Database driver updates may affect CTE behavior (e.g., PostgreSQL syntax).
  • Debugging:
    • Use toSql() to inspect generated CTE queries.
    • Log query execution plans for recursive CTEs (e.g., EXPLAIN ANALYZE in PostgreSQL).

Support

  • Community Resources:
    • 665 stars, active maintenance (last release: 2026-02-28).
    • GitHub issues resolved promptly (e.g., PHP 8.4 support, Oracle fixes).
  • Documentation:
    • Comprehensive README with usage examples.
    • Database-specific notes (e.g., cycle detection limitations).
  • Training Needs:
    • SQL proficiency recommended for advanced CTEs.
    • Lumen/Oracle users need extra setup guidance.

Scaling

  • Performance Considerations:
    • Recursive CTEs: Risk of stack overflow or high memory usage for deep hierarchies (e.g., >100 levels). Monitor with EXPLAIN.
    • Materialized CTEs: May increase memory usage but improve read performance.
    • Database Limits: Some DBs (e.g., MySQL) have CTE recursion limits (default: 1000).
  • Horizontal Scaling:
    • CTEs are query-level optimizations; scaling depends on underlying DB infrastructure.
    • Read replicas may benefit from materialized CTEs for reporting.
  • Cost Implications:
    • No direct cost, but complex CTEs may increase CPU/memory usage on DB tier.

Failure Modes

Failure Scenario Impact Mitigation
Unsupported database Queries fail silently or incorrectly. Validate DB compatibility early; use feature flags for uns
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata