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 2w 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 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

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://avatars.githubusercontent.com/u/2896847?v=4)[Eric Busch](/maintainers/etbusch)[@etbusch](https://github.com/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

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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