Download the PHP package weblogin/laravel-lookup without Composer
On this page you can find all versions of the php package weblogin/laravel-lookup. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download weblogin/laravel-lookup
More information about weblogin/laravel-lookup
Files in weblogin/laravel-lookup
Package laravel-lookup
Short Description Laravel lookup objects and Eloquent casts for static data.
License MIT
Homepage https://github.com/weblogin/laravel-lookup
Informations about the package laravel-lookup
⚠️ This Package was originally created on Laravel 7 because it didn't offer this kind of feature. Since Laravel 9 we can use Enum Eloquent attribute casting, in most cases they can do the same as Laravel-Lookup so we recommend giving them a try instead of our package.
Laravel Lookup
Laravel Lookup helps to have a place to store static data of your Laravel application with little sugar added to it and Eloquent casts to easily use them in your Laravel models. Useful when you don't want to create database tables for every little things, think of it as static model.
Imagine you have a Car
model with an attribute engine
that can have the value gasoline
, electric
or hybrid
. Creating a model and a database table can be overkill for this simple key/value data.
In the past we used language files to store this but would end up having to deal with a simple array when calling Lang::get('car.engines')
. It felt weird and not realy DRY when needing to add some reusable logic for these data. The main goal was having one stable source to get this data and be able to have some flexibility in it.
With a lookup object (other call it Value object) you'll be able to have a collection containing real objects. It lets you use the power of Laravel collection to easily populate a dropdown select, add form validation, populate factories, etc and with Laravel custom cast we can access them like a real model relation for a nice code readability.
- Installation
- Usage of Lookup
- Usage of Casts
- Configuration
Installation
You can install the package via composer :
Usage of Lookup
With our Car
model example that have a engine
attribute we could create a CarEngineLookup
in App/Models/Lookups
. It should look like bellow, with a getItems()
method returning an array, each item of the array should contain the same keys and they need to be declared as public in the class :
Because getItems()
have to return a simple array you can use this package with translation as you like, for example :
A Lookup
object can be used as a collection by calling any static method on it, so you can do :
The find()
method should be used to find one or multiple lookups in the collection (return null if nothing found) :
Populating random values inside model factories
Populating select input and Validation :
Because a Lookup
is an object you can add attributes (they need to be declared in the class and in the getItems()
method) and helper methods on the class :
Usage of Casts
There are 2 casts in this package, both of them uses the primary key of the lookup object to get and set values from the database (go to the Configuration section to read more about the primary key) :
LookupCast
to store one lookup key in the databaseLookupCollectionCast
to store multiple lookup keys in the database
LookupCast
Still with our Car
model that have an engine
attribute, the model would look like this to use the CarEngineLookup
stored in App/Models/Lookups
:
In your code you can now access the engine
attribute object :
Note: If the key is not in the collection the attribute equals to NULL.
LookupCollectionCast
Now our Car
model needs a options
attribute that should store multiple options coming from a lookup CarOptionLookup
:
In your code you can now access the options
attribute that return a collection :
Note: If the keys are not in the collection the attribute equals to NULL.
Search in database
For LookupCast
you can do a simple where()
query, nothing special :
For LookupCollectionCast
, the values are stored in the database as JSON so you can use whereJsonContains()
but if you pass multiple keys this method search for the presence of each key. To search the presence of at least one key the package include a new database query method whereInLookupCollection($column, $values)
:
Configuration
Database column type
Set database column that will store the value according to your Lookup
usage, VARCHAR
for single value and JSON
for multiple values.
Primary key
By default a Lookup
needs to have key
attribute defined for each item and it needs to be unique (it can by a string
or int
). It's used to retrieve the object from the collection. But you can override this primary key by adding a $primaryKeyName
attribute to the class :
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-lookup with dependencies
illuminate/contracts Version ^7.0|^8.0|^9.0|^10.0
illuminate/database Version ^7.0|^8.0|^9.0|^10.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0