trexology / extendable
Laravel 用于添加和管理自定义 Eloquent 模型字段的特质。
1.5.3
2019-03-13 07:30 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.0
README
如何安装
Composer 安装
composer require trexology/extendable
Laravel 服务提供者(对于 5.5 版本以上的 Laravel 不必使用)
在 app/config/app.php
中添加服务提供者
'providers' => [ Trexology\Extendable\Providers\ExtendableServiceProvider::class, ];
发布配置、模板并运行迁移。
php artisan vendor:publish --provider="Trexology\Extendable\Providers\ExtendableServiceProvider" php artisan migrate
用法
添加特质
将模型特质添加到需要使用自定义字段的模型中。
use Trexology\Extendable\Traits\Extendable; class Article extends \Illuminate\Database\Eloquent\Model { use Extendable; }
配置字段
使用 app/config/custom-fields.php
来配置你的字段。
return [ 'App\User' => [ // model name 'gender' => [ // field name 'title' => 'Gender', // field title (can be used in views) 'type' => \Trexology\Extendable\CustomFieldType::Radio, // field type 'options' => [ // possible values/labels 'male' => 'Male', 'female' => 'Female' ], 'default' => 'male' // default value ] ] ];
分配/检索自定义字段值
将自定义字段值作为常规值分配。
$data = [ 'title' => 'Awesome Article!!!', // regular field 'gender' => 'male' // custom filed ]; $article = new Article(); $article->fill($data); $article->save();
检索自定义字段值。
$article = Article::find(1); $article->gender->value; // 1 echo $article->gender; // 1 $article->customFields; // return collection of custom fields