PHPackages                             paulhenri-l/eloquent-builder-decorator - 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. paulhenri-l/eloquent-builder-decorator

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

paulhenri-l/eloquent-builder-decorator
======================================

Allows you to decorate Laravel query builders

2.0.0(5y ago)1114.5k1MITPHPPHP ^7.3CI failing

Since Jul 21Pushed 5y agoCompare

[ Source](https://github.com/paulhenri-l/eloquent-builder-decorator)[ Packagist](https://packagist.org/packages/paulhenri-l/eloquent-builder-decorator)[ RSS](/packages/paulhenri-l-eloquent-builder-decorator/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (5)Versions (5)Used By (0)

Eloquent Builder Decorator
==========================

[](#eloquent-builder-decorator)

[![Build Status](https://camo.githubusercontent.com/ba5a71174dcbc5073e4f2af542b05551350c5dfc7b9a138df554650b6a15a363/68747470733a2f2f7472617669732d63692e6f72672f7061756c68656e72692d6c2f656c6f7175656e742d6275696c6465722d6465636f7261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/paulhenri-l/eloquent-builder-decorator)

This package will allow you to create Query builder decorators.

### Why decorating the query builder?

[](#why-decorating-the-query-builder)

Decorating a query builder might be useful if you are creating a package that accepts a query builder as an argument and want to call the same complex query on it at multiple places.

It is also useful if you want to share common queries between multiple projects.

### Why using this package instead of the query builder macro system?

[](#why-using-this-package-instead-of-the-query-builder-macro-system)

While being very useful the macro system is global.

If your package adds macros to the query builder, then, your code is having a side effect on the application of your user.

There are cases where this might be wanted, but if it is not, decorating is a safer approach.

Installation
------------

[](#installation)

```
composer require paulhenri-l/eloquent-builder-decorator

```

Usage
-----

[](#usage)

### Creating a decorator

[](#creating-a-decorator)

In order to create a decorator you need to create a new class and make it extend `PaulhenriL\EloquentBuilderDecorator\AbstractBuilderDecorator`.

Inside of this class is the `queryToDecorate` property. This property contains the builder instance that you are decorating.

```
class QueryWithActive extends PaulhenriL\EloquentBuilderDecorator\AbstractBuilderDecorator
{
    public function whereActive()
    {
        return static::decorate(
            $this->queryToDecorate->where('active', true)
        );
    }
}
```

### Using a decorator

[](#using-a-decorator)

Now that you have created your decorator you can use it like so:

```
$usersQuery = User::query();
$usersQuery = new QueryWithActive($query);

// We can now call whereActive() on the query.
$activeUsersCount = $usersQuery->whereActive()->count();
```

### Chaining multiple decorators

[](#chaining-multiple-decorators)

Decorators become really powerful when you chain them together. In this example we'll chain the fictional decorators QueryWithDates and QueryWithRegion.

Now that they are chained our query is extended with the methods of each one of them.

We can call their methods as many times as we want and in any order.

```
$podcastsQuery = \DB::table('podcasts');

$podcastsQuery = new QueryWithDates($podcastsQuery);
$podcastsQuery = new QueryWithRegion($podcastsQuery);

$podcasts = $podcastsQuery->where('host', 'someone')
    ->happeningToday()
    ->inEurope()
    ->take(10)
    ->get();
```

### Caveats

[](#caveats)

If you want to keep the ability to chain decorators you have to make sure that the methods you create in your decorators are returning decorator instances and not builder instances.

So you should do:

```
public function whereActive()
{
    return static::decorate(
        $this->queryToDecorate->where('active', true)
    );
}
```

and you shouldn't do:

```
public function whereActive()
{
    return $this->queryToDecorate->where('active', true)
}
```

The `static::decorate($query)` method is just a bit of syntactic sugar what it really does is `new static($query)`.

Contributing
------------

[](#contributing)

If you have any questions about how to use this library feel free to open an issue.

If you think that the documentation or the code could be improved in any way open a PR and I'll happily review it!

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Every ~155 days

Total

4

Last Release

2025d ago

Major Versions

1.2.0 → 2.0.02020-10-27

PHP version history (4 changes)1.0.0PHP ^7.1.3

1.1.0PHP ^7.2

1.2.0PHP ^7.2.5

2.0.0PHP ^7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/dadbbf7b24bb9ee393f7a03b8c6c611e3f922096be33ac881e4641f744d12eb4?d=identicon)[paulhenri-l](/maintainers/paulhenri-l)

---

Top Contributors

[![paulhenri-l](https://avatars.githubusercontent.com/u/25308170?v=4)](https://github.com/paulhenri-l "paulhenri-l (12 commits)")

---

Tags

decoratoreloquentlaravelquery-builder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paulhenri-l-eloquent-builder-decorator/health.svg)

```
[![Health](https://phpackages.com/badges/paulhenri-l-eloquent-builder-decorator/health.svg)](https://phpackages.com/packages/paulhenri-l-eloquent-builder-decorator)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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