PHPackages                             lorisleiva/laravel-add-select - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lorisleiva/laravel-add-select

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lorisleiva/laravel-add-select
=============================

v0.1.4(5y ago)325.5k3MITPHP

Since Jan 11Pushed 5y ago2 watchersCompare

[ Source](https://github.com/lorisleiva/laravel-add-select)[ Packagist](https://packagist.org/packages/lorisleiva/laravel-add-select)[ RSS](/packages/lorisleiva-laravel-add-select/feed)WikiDiscussions main Synced yesterday

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

Laravel Add Select
==================

[](#laravel-add-select)

🧱 Add subSelect queries to your Laravel models using dynamic methods.

If you're not familiar with subSelect queries, I strongly recommend [this article by Johnathan Reinink](https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries#can-this-be-done-with-a-has-one).

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

[](#installation)

```
composer require lorisleiva/laravel-add-select
```

Usage
-----

[](#usage)

Consider two Eloquent models `Book` and `Chapter` such that a book can have multiple chapters.

By using the `AddSubSelects` trait, you can now add subSelect queries to the `Book` model by following the naming convention `add{NewColumnName}Select`. For example, the following piece of code add two new subSelect queries to the columns `last_chapter_id` and `latest_version`.

```
class Book extends Model
{
    use AddSubSelects;

    public function addLastChapterIdSelect()
    {
        return Chapter::select('id')
            ->whereColumn('book_id', 'books.id')
            ->latest();
    }

    public function addLatestVersionSelect()
    {
        return Chapter::select('version')
            ->whereColumn('book_id', 'books.id')
            ->orderByDesc('version');
    }
}
```

Now, you can eager-load these subSelect queries using the `withSelect` method.

```
Book::withSelect('last_chapter_id', 'latest_version')->get();
```

You can also eager-load models that are already in memory using the `loadSelect` method. Note that this method will load all provided subSelect queries in one single database query.

```
$book->loadSelect('last_chapter_id', 'latest_version');
```

If you haven't eager-loaded these subSelect queries in a model, you can still access them as attributes. The first time you access them, They will cause a new database query but the following times they will be available in the model's attributes.

```
$book->last_chapter_id;
$book->latest_version;
```

Finally, you can gloabally eager-load these subSelect queries by setting up the `withSelect` property on the Eloquent model.

```
class Book extends Model
{
    use AddSubSelects;

    public $withSelect = ['last_chapter_id', 'latest_version'];
}
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity50

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

Total

5

Last Release

2030d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/284b8d85b9924296c19360effa1758a9f534b12a6bc0235fd31ab70c6bc63318?d=identicon)[lorisleiva](/maintainers/lorisleiva)

---

Top Contributors

[![lorisleiva](https://avatars.githubusercontent.com/u/3642397?v=4)](https://github.com/lorisleiva "lorisleiva (8 commits)")

### Embed Badge

![Health badge](/badges/lorisleiva-laravel-add-select/health.svg)

```
[![Health](https://phpackages.com/badges/lorisleiva-laravel-add-select/health.svg)](https://phpackages.com/packages/lorisleiva-laravel-add-select)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

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