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 3w ago

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

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

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

2081d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3642397?v=4)[Loris Leiva](/maintainers/lorisleiva)[@lorisleiva](https://github.com/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

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M282](/packages/illuminate-pipeline)[illuminate/redis

The Illuminate Redis package.

8314.6M375](/packages/illuminate-redis)[spatie/laravel-export

Create a static site bundle from a Laravel app

674146.0k6](/packages/spatie-laravel-export)

PHPackages © 2026

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