lorisleiva/laravel-add-select
Larablocks Analysis
Compatibility
Trusted maintainerlorisleiva/laravel-add-select works with Laravel 5 to 8 (latest release v0.1.4, Oct 21, 2020), all past end of life (the last on Jan 24, 2023): no more fixes, even for security.
- Trusted maintainer: lorisleiva publishes 6 packages on Larablocks, with 1,851,769 downloads a month between them.
Recommendation checks
Not advisedNot advised: no release or commit in over 2 years.
- No release or commit in over 2 years
- Supports only Laravel 5 to 8, past end of life since Jan 24, 2023
- No release in the last 12 months (last: Oct 21, 2020)
- 0 downloads a month (needs 10,000)
- 32 GitHub stars (needs 100)
State
Likely abandoned- No release or commit in over 2 years.
- Last stable release: Oct 21, 2020.
- No releases in the last 12 months.
- First released: Jan 11, 2020.
- 0 open issues on GitHub.
Data from Packagist and GitHub, checked by Larablocks on Nov 29, 2026.
| Install | |
|---|---|
composer require lorisleiva/laravel-add-select |
|
| Latest Version: | v0.1.4 |
| Laravel: | 5 – 8 |
| License: | MIT |
| Last Updated: | Dec 3, 2020 |
| Links: | GitHub · Packagist |
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.
Installation
composer require lorisleiva/laravel-add-select
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'];
}
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | Laravel | License |
|---|---|---|---|
| v0.1.4 | ^5.8| | MIT | |
| v0.1.3 | ^5.8| | MIT | |
| v0.1.2 | 5.8.*| | MIT | |
| v0.1.1 | 5.8.*| | MIT | |
| v0.1 | 5.8.*| | MIT |