chrisabey84/laravel-cached-options-list
Composer 安装命令:
composer require chrisabey84/laravel-cached-options-list
包简介
A simple package that allows you to cache all rows on a relationship model for use in select arrays on forms
README 文档
README
A simple package that allows you to cache all rows on a relationship model for use in select arrays on forms.
Installation
You can install the package via composer:
composer require chrisabey84/laravel-cached-options-list
You can publish the config file with:
php artisan vendor:publish --provider="Chrisabey84\LaravelCachedOptionsList\LaravelCachedOptionsListServiceProvider
This is the contents of the published config file:
return [ 'key' => 'id', //The index/key column for the select options array 'value' => 'name', //The value column for the select options array ];
Usage
Adding the HasCachedOptionsList trait to any of your models will provide the following functionality:
$rows = \App\Models\MyModel::asSelectArray();
Will retrieve all rows as an associative array which can then be used in your blade templates to populate the option in a select box:
<select name="mySelectBox"> @foreach($rows as $key => $value) <option value="{{ $key }}">{{ $value }}</option> @endforeach </select>
Custom Behavior
By default asSelectArray() will retrieve all rows from the database table however, you can customise this behavior by overriding the following method in your model which must return a Builder instance:
protected static function buildQuery(): Builder { return static::query(); }
Clearing The Cache
The cache will be automatically cleared when creating, updating or deleting your models.
To manually clear the cache, you can either call:
\App\Models\MyModel::clearOptionsCache();
Or use the handy artisan command with the model name as the argument:
php artisan cached-options:clear '\App\Models\MyModel'
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 305
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-04