PHPackages                             etbusch/postgres-ctes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. etbusch/postgres-ctes

ActivePackage[Database &amp; ORM](/categories/database)

etbusch/postgres-ctes
=====================

Laravel addon to provide native support for PostgresSQL Common Table Expressions in Eloquent's query builder.

16PHP

Since Mar 31Pushed 7y ago1 watchersCompare

[ Source](https://github.com/etbusch/postgres-ctes)[ Packagist](https://packagist.org/packages/etbusch/postgres-ctes)[ RSS](/packages/etbusch-postgres-ctes/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel PostgresSQL CTE Driver Extenstion
=========================================

[](#laravel-postgressql-cte-driver-extenstion)

This package adds Common Table Expression support to the base PostgresSQL driver in Laravel 5.4+ and Lumen 5.

Install
-------

[](#install)

#### Via Composer and local git repository:

[](#via-composer-and-local-git-repository)

Clone the repository to a local directory on your server/workstation.

Add the following to your app's composer.json:

```
"repositories": [{
    "type": "vcs",
    "url": "path/to/postgres-ctes"
}]
```

Then run:

```
$ composer require etbusch\postgres-ctes
```

#### Via Composer and Packagist

[](#via-composer-and-packagist)

Simply run:

```
$ composer require etbusch\postgres-ctes
```

Once composer has been updated and the package has been installed, the service provider will need to be loaded.

For Lavavel 5.5+, the package will autodiscover the needed service provider, so no additional steps are necessary.

For Laravel 5.4, open `config/app.php` and add following line to the providers array:

```
Etbusch\PostgresCtes\PostgresCtesServiceProvider::class,
```

For Lumen 5, open `bootstrap/app.php` and add following line under the "Register Service Providers" section:

```
$app->register(Etbusch\PostgresCtes\PostgresCtesServiceProvider::class);
```

Usage
-----

[](#usage)

Once you include the service provider Laravel/Lumen will start using the custom grammar and the
`QueryBuilder::cte($as, Closure $query)` method will be available for use in your Models. The CTE is named with the first argument and the query itself is represented in the second argument as a closure or a string of raw SQL.

### Basic Example

[](#basic-example)

```
$query = new FooModel();
$data = $query->cte('cte_query_name', function ($query) {
    $query->select('foo')
    ->from('bar')
    ->selectRaw('count(*) as total')
    ->selectRaw('count(case when foo_bar = 1 then 1 end) AS Low')
    ->selectRaw('count(case when foo_bar = 2 then 1 end) AS Medium')
    ->selectRaw('count(case when foo_bar = 3 then 1 end) AS High')
    ->groupBy('foo');
  })
  ->from('cte_query_name')
  ->limit(20)
  ->get();
```

### Query Scope Example

[](#query-scope-example)

```
// Model Scope Method
public function scopeCteQueryName($query){
    $cte = DB::table('foo')
    ->select('foo')
    ->from('bar')
    ->selectRaw('count(*) as total')
    ->selectRaw('count(case when foo_bar = 1 then 1 end) AS Low')
    ->selectRaw('count(case when foo_bar = 2 then 1 end) AS Medium')
    ->selectRaw('count(case when foo_bar = 3 then 1 end) AS High')
    ->groupBy('foo');

    $query->cte('cte_query_name',$cte);
    return $query;
}

// Usage
$query = new FooModel();
$data = $query->CteQueryName()
  ->from('cte_query_name')
  ->limit(10)
  ->get();
```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfc76ba0a0629b303f4e99e14cfa1e0356df4bebfe78b6a9c82cbf8b9a0db243?d=identicon)[etbusch](/maintainers/etbusch)

---

Top Contributors

[![etbusch](https://avatars.githubusercontent.com/u/2896847?v=4)](https://github.com/etbusch "etbusch (15 commits)")

### Embed Badge

![Health badge](/badges/etbusch-postgres-ctes/health.svg)

```
[![Health](https://phpackages.com/badges/etbusch-postgres-ctes/health.svg)](https://phpackages.com/packages/etbusch-postgres-ctes)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
