PHPackages                             faliure/laravel-custom-builder - 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. faliure/laravel-custom-builder

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

faliure/laravel-custom-builder
==============================

Custom Eloquent Builders to manipulate its results as defined

1.0.2(2y ago)0731MITPHP

Since Feb 4Pushed 2y ago1 watchersCompare

[ Source](https://github.com/faliure/laravel-custom-builder)[ Packagist](https://packagist.org/packages/faliure/laravel-custom-builder)[ RSS](/packages/faliure-laravel-custom-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (1)

faliure/laravel-custom-builder
==============================

[](#faliurelaravel-custom-builder)

Laravel Custom Builder allows you to perform Eloquent queries as you normally would, but changing the shape or type of its response.

---

API
---

[](#api)

The Builder class can be initialized with a number of different types as the first argument:

- From an Eloquent Builder

    - The custom Builder will just use the Elqouent Builder in its current state (which may include some operations already performed on it).
- From an Eloquent Model

    - The underlying Eloquent Builder will be a fresh one for this Model instance (you could, for example, fetch relations and continue the chain from there)
- From an Eloquent Model's class (FQN)

    - The underying Eloquen Builder will be a fresh one for this Model class

The second argument, optional, is a generic `$callback` that will apply to any final methods of the Eloquent chain (recognized final methods are `first()`, `get()`, `count()` and `pluck()`). It's rarely the case that you would use a single callback for all final methods, but in some cases it could make sense (see the first trivial example below).

Normally, you would not use the second argument, but instead would define different callbacks for different final methods. This is done by calling `Builder@setCallback()` or `Builder@setCallbacks()`, as in the following examples:

```
use Faliure\LaravelCustomBuilder\Builder;

# Generic callback for all final methods
$builder = new Builder(User::class, fn ($x) => $x->toArray());

# Custom callback, only for `get()`
# NOTE: the other final methods don't transform the result
$builder = new Builder(User::class);
$builder->setCallback($someCallback, Builder::GET);

# Custom callback for `get()` and `first()`
# NOTE: the other final methods don't transform the result
$builder = new Builder(User::class);
$builder->setCallbacks($someCallback, [ Builder::GET, Builder::FIRST ]);
```

---

Usage
-----

[](#usage)

Basic example (just for illustration, not really useful):

```
use App\Models\User;
use Faliure\LaravelCustomBuilder\Builder;

$builder = new Builder(User::class, fn ($result) => $result->toArray());

$userData = $builder->first();
$usersData = $builder->where('status', 'active')->get();
```

In both cases, the result is not the Model or an Eloquent Collection of Models, but instead the array representations of both.

Let's see a more realistic example. Here, we have a transform option that comes from elsewhere, and we just plug it here using the Custom Builder:

```
use App\Models\User;
use Faliure\LaravelCustomBuilder\Builder;

$transform = TransformsFactory::getSomeUserTransformFn(); // Returns some hypotetical transformation closure

$builder = new Builder(User::class, $transform);
$usersTransformed = $builder->get();
```

In this case, without the Custom Builder we would have done something like:

```
use App\Models\User;
use Faliure\LaravelCustomBuilder\Builder;

$transform = TransformsFactory::getSomeUserTransformFn(); // Returns some hypotetical transformation closure

$usersTransformed = $transform(User::all());
```

Where it really gets interesting is when you can build your own custom functions that leverage the Builder to seamlessly transform regular Eloquent results into something else (see, for example, [faliure/laravel-resourceable](https://github.com/faliure/laravel-resourceable)).

```
trait HasResources // Eloquent Model trait
{
    public static function resourcesQuery(?string $resourceClass = null): Builder
    {
        $resourceClass ??= static::resourceClass();

        return (new Builder(static::class))
            ->setCallback(
                fn ($models) => $resourceClass::collection($models),
                Builder::GET
            )->setCallback(
                fn ($model) => $model->resource(),
                Builder::FIRST
            );
    }
}

# Elsewhere in the code
$resources = User::resourcesQuery()->where('some', 'constraint')->get();
```

With this example, we can package the complexity of creating the builder and providing it the callback(s), and just create some handy functions with it.

---

Customization
-------------

[](#customization)

By default, Resourceable assumes your main Model JsonResource follows the standard:

- User's main Resource is `App\Http\Resources\UserModel`

If that's not the case, you may define a property `resourceClass` in your Resourceable Modelm, like so:

```
class User implements Resourceable
{
    // ...

    protected string $resourceClass = TheResourceClass::class;
}
```

Moreover, you can fetch different kinds of Resources for a model, by passing an optinal `$resourceClass` parameter to any of the methods defined in the interface. Namely:

```
$resource = $user->resource(SomeOtherResourceType::class);

$resources = User::resources(SomeOtherResourceType::class);

$filteredResources = User::resourcesQuery(SomeOtherResourceType::class)
    ->where('some', 'constraint')
    // ->...
    ->get();
```

It's just as simple as that!

### How does the `resourcesBuilder` work?

[](#how-does-the-resourcesbuilder-work)

The `resourcesBuilder` is a Custom Eloquent Builder that allows you to write Eloquent chains as you normally would, but it modifies the end result to return the associated Resources instead of a Model or a collection of them.

Check [faliure/laravel-custom-builder](https://github.com/faliure/laravel-custom-builder) for more information and other usages.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~205 days

Total

3

Last Release

781d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2d47a0714c283109f82f0ddb6d34c8898408db5180c61bca65d72452b737c404?d=identicon)[faliure](/maintainers/faliure)

---

Top Contributors

[![dbarreiro](https://avatars.githubusercontent.com/u/462973?v=4)](https://github.com/dbarreiro "dbarreiro (5 commits)")[![faliure](https://avatars.githubusercontent.com/u/714232?v=4)](https://github.com/faliure "faliure (1 commits)")

### Embed Badge

![Health badge](/badges/faliure-laravel-custom-builder/health.svg)

```
[![Health](https://phpackages.com/badges/faliure-laravel-custom-builder/health.svg)](https://phpackages.com/packages/faliure-laravel-custom-builder)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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