PHPackages                             reinink/advanced-eloquent - 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. reinink/advanced-eloquent

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

reinink/advanced-eloquent
=========================

A set of advanced Eloquent macros for Laravel

v0.2.0(7y ago)578461.2k—0.9%23[1 issues](https://github.com/reinink/advanced-eloquent/issues)1MITPHP

Since Nov 22Pushed 5y ago12 watchersCompare

[ Source](https://github.com/reinink/advanced-eloquent)[ Packagist](https://packagist.org/packages/reinink/advanced-eloquent)[ Docs](https://github.com/reinink/advanced-eloquent)[ RSS](/packages/reinink-advanced-eloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (1)

Advanced Eloquent
=================

[](#advanced-eloquent)

A set of advanced Eloquent macros for Laravel.

> ⚠️ Note, I've brought much of the functionality provided by this package to Laravel core, in particular the subquery functionality, which has pretty much made this package obsolete. If you want to learn more about these features, be sure to see my [Eloquent Peformance Patterns](https://eloquent-course.reinink.ca/) course, which covers these techniques and others in detail.

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

[](#installation)

You can install this package via Composer:

```
composer require reinink/advanced-eloquent

```

This package uses auto-discovery, so there is no further configuration required.

API
---

[](#api)

### `addSubSelect($column,  $query)`

[](#addsubselectcolumn--query)

- `$column` must be a string.
- `$query` must either be an instance of `Illuminate\Database\Query\Builder` or `Illuminate\Database\Eloquent\Builder`.

### `orderBySub($query, $direction = 'asc', $nullPosition = null)`

[](#orderbysubquery-direction--asc-nullposition--null)

- `$query` must either be an instance of `Illuminate\Database\Query\Builder` or `Illuminate\Database\Eloquent\Builder`.
- `$direction` must either be `'asc'` or `'desc'`.
- `$nullPosition` must either be `null`, `'first'` or `'last'`.

### `orderBySubAsc($query, $nullPosition = null)`

[](#orderbysubascquery-nullposition--null)

- `$query` must either be an instance of `Illuminate\Database\Query\Builder` or `Illuminate\Database\Eloquent\Builder`.
- `$nullPosition` must either be `null`, `'first'` or `'last'`.

### `orderBySubDesc($query, $nullPosition = null)`

[](#orderbysubdescquery-nullposition--null)

- `$query` must either be an instance of `Illuminate\Database\Query\Builder` or `Illuminate\Database\Eloquent\Builder`.
- `$nullPosition` must either be `null`, `'first'` or `'last'`.

*Note: Null positions (`NULLS FIRST` and `NULLS LAST`) are not supported by all databases (ie. MySQL and SQLite), but are supported by PostgreSQL and others.*

Examples
--------

[](#examples)

Get a user's last login date using a subquery:

```
$users = User::addSubSelect('last_login_at', Login::select('created_at')
    ->whereColumn('user_id', 'users.id')
    ->latest()
)->get();
```

Same example as above, except using the query builder instead:

```
$users = DB::table('users')->addSubSelect('last_login_at', DB::table('logins')
    ->select('created_at')
    ->whereColumn('user_id', 'users.id')
    ->latest()
)->get()
```

Order users by their company name using a subquery:

```
$users = User::orderBySub(Company::select('name')->whereColumn('company_id', 'companies.id'))->get();
```

Order users by their last login date, with null values last:

```
$users = User::addSubSelect('last_login_at', Login::select('created_at')
        ->whereColumn('user_id', 'users.id')
        ->latest()
    )->orderBySubDesc(Login::select('created_at')
        ->whereColumn('user_id', 'users.id')
        ->latest(), 'last'
    )->get();
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity56

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity52

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 ~1 days

Total

2

Last Release

2733d ago

### Community

Maintainers

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

---

Top Contributors

[![reinink](https://avatars.githubusercontent.com/u/882133?v=4)](https://github.com/reinink "reinink (10 commits)")

---

Tags

laraveleloquentadvancedmacro

### Embed Badge

![Health badge](/badges/reinink-advanced-eloquent/health.svg)

```
[![Health](https://phpackages.com/badges/reinink-advanced-eloquent/health.svg)](https://phpackages.com/packages/reinink-advanced-eloquent)
```

###  Alternatives

[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[mpyw/laravel-local-class-scope

A tiny macro that reuse a global scope class as a local scope

24102.6k](/packages/mpyw-laravel-local-class-scope)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[cybercog/laravel-nova-ban

A Laravel Nova banning functionality for your application.

40199.8k](/packages/cybercog-laravel-nova-ban)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

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