Download the PHP package pascalvgemert/laravel-lookupable without Composer
On this page you can find all versions of the php package pascalvgemert/laravel-lookupable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pascalvgemert/laravel-lookupable
More information about pascalvgemert/laravel-lookupable
Files in pascalvgemert/laravel-lookupable
Package laravel-lookupable
Short Description Lookupable Trait for Laravel eloquent models for quick and efficient table lookups.
License MIT
Homepage https://github.com/pascalvgemert/laravel-lookupable
Informations about the package laravel-lookupable
Laravel Lookupable
Lookupable Trait for Laravel eloquent models for quick and efficient table lookups.
Requires Laravel 5.5 or higher!
About the package
When building more complex applications, lookup queries can add up without you even knowing. The lookupable trait will only get the lookup instances once per request and store them in memory. Every next time you want to lookup a lookupable instance of the same Model, the lookup methods will access the in memory stored instances and prevent a query.
Example:
You have a lookup table for statuses like pending
, draft
, published
, deleted
. You can access these instances multiple times, with only one query per request:
Installation
You can install the package via composer:
Usage
The Lookupable
trait can be used only for Eloquent Models
Example table scheme for 'Roles'
id | identifier | title |
---|---|---|
1 | admin | Admin User |
2 | user | System User |
3 | guest | Guest User |
Lookupable methods
After this you can lookup instances easily with the following methods:
Single Lookup
Single Lookup which throws an \Illuminate\Database\Eloquent\ModelNotFoundException error when no record could be found
Multiple Lookup
Multiple Lookup which throws an \Illuminate\Database\Eloquent\ModelNotFoundException error when any of the given record could NOT be found
Soft Deleted items
All of the above methods can except a second parameter (bool $withTrashed = false;
), to also return with Soft Deleted items.
Note: When trying to use the Soft Deleted items, please make sure your Eloquent Model implements the \Illuminate\Database\Eloquent\SoftDeletes trait.
I don't have an identifier
column?
So your database table doesn't contain an identifier
column and looks like this Country
table for example:
id | code | title |
---|---|---|
1 | NL | Netherlands |
2 | UK | Great Brittain |
3 | BE | Belgium |
4 | US | United States |
5 | ... | ... |
No worries, you can define your own lookup column name in your model like so:
Credits
- Pascal van Gemert
- Kevin op den Kamp (for inspiration)
- Startselect