1. Go to this page and download the library: Download weblogin/laravel-lookup library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
weblogin / laravel-lookup example snippets
namespace App\Models\Lookups;
use WebLogin\LaravelLookup\Lookup;
class CarEngineLookup extends Lookup
{
public $key;
public $name;
protected static function getItems()
{
return [
['key' => 'gas', 'name' => "Gasoline"],
['key' => 'elec', 'name' => "Electric"],
['key' => 'hyb', 'name' => "Hybrid"],
];
}
}
// Get all the CarEngineLookup objects in a collection
CarEngineLookup::collection();
// Filter on the collection (both are equals)
CarEngineLookup::where('key', '!=', 'gas');
CarEngineLookup::collection()->where('key', '!=', 'gas');
class CarEngineLookup extends Lookup
{
public $key;
public $name;
public $sub;
protected static function getItems()
{
return [
['key' => 'gas', 'name' => "Gasoline", 'sub' => 'Lorem ipsum'],
['key' => 'elec', 'name' => "Electric", 'sub' => 'Dolor amet'],
['key' => 'hyb', 'name' => "Hybrid", 'sub' => 'Consectetur adipiscing'],
];
}
public function icon()
{
return URL::asset("images/engines-" . $this->key . ".svg");
}
public function fullname()
{
return $this->name . ' - ' . $this->sub;
}
public static function onlyGreen()
{
return static::collection()->where('key', '!=', 'gas');
}
}
$engine = CarEngineLookup::find('gas');
$engine->icon(); // 'yourdomaine.com/images/engines-gas.svg'
$engine->fullname(); // 'Gasoline - Lorem ipsum'
CarEngineLookup::onlyGreen(); // Collection without gas item
...
use WebLogin\LaravelLookup\LookupCast;
class Car extends Model
{
protected $casts = [
'engine' => LookupCast::class.':'. Lookups\CarEngineLookup::class,
];
}
use WebLogin\LaravelLookup\Lookup;
class CarOptionLookup extends Lookup
{
public $key;
public $name;
protected static function getItems()
{
return [
['key' => 'airbag', 'name' => "Airbag"],
['key' => 'abs', 'name' => "Antilock braking system"],
['key' => 'rear-cam', 'name' => "Rear camera"],
['key' => 'front-cam', 'name' => "Font camera"],
];
}
}
use WebLogin\LaravelLookup\LookupCollectionCast;
class Car extends Model
{
protected $casts = [
...
'options' => LookupCollectionCast::class.':'. Lookups\CarOptionLookup::class,
];
}
$car = new Car();
// Set multiple keys
$car->options = ['airbag', 'abs'];
$car->options; // Collection
$car->options->first()->name; // 'Airbag'
// Set one key (both are equals)
$car->options = ['airbag'];
$car->options = 'airbag';
// You can also use Lookup objects
$car->options = CarOptionLookup::find('airbag');
Car::where('engine', 'elec')->get();
Car::whereInLookupCollection('options', 'airbag')->get();
Car::whereInLookupCollection('options', ['rear-cam', 'abs'])->get();
// You can also pass Lookup object
$foo = CarOptionLookup::find('airbag');
Car::whereInLookupCollection('options', $foo)->get();
class CountryLookup extends Lookup
{
public $code;
public $name;
protected static $primaryKeyName = 'code';
protected static function getItems()
{
return [
['code' => 'fra', 'name' => "France"],
['code' => 'usa', 'name' => "United States of America"],
...
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.