ironshark/laravel-extendable

Laravel 的 traits,用于添加和管理自定义 Eloquent 模型字段。

v1.0.10 2016-07-12 11:54 UTC

README

License Downloads Version-stable

安装方法

Composer 安装

composer require ironshark/laravel-extendable

Laravel 服务提供者

app/config/app.php 中添加服务提供者

'providers' => [
    IronShark\Extendable\ExtendableServiceProvider::class,
];

发布配置、模板并运行迁移。

php artisan vendor:publish --provider="IronShark\Extendable\ExtendableServiceProvider"
php artisan migrate

使用方法

添加 traits

将模型 trait 添加到模型中,您希望使用自定义字段的位置。

class Article extends \Illuminate\Database\Eloquent\Model {
    use IronShark\Extendable\ModelTrait;
}

配置字段

使用 app/config/custom-fields.php 配置您的字段。

return [
    'App\Room' => [                                                     // model name
        'light' => [                                                    // field name
            'title' => 'Light',                                         // field title (can be used in views)
            'type' => \IronShark\Extendable\CustomFieldType::Radio,     // field type
            'options' => [                                              // possible values/labels
                0 => 'Off',
                1 => 'On'
            ],
            'default' => 1                                              // default value
        ]
    ]
];

分配/检索自定义字段值

将自定义字段值分配为常规值。

$data = [
    'title' => 'Awesome Article!!!', // regular field
    'recomended' => 1                // custom filed     
];

$article = new Article();
$article->fill($data);
$article->save();

检索自定义字段值。

$article = Article::find(1);
$article->recomended->value; // 1
echo $article->recomended;   // 1

字段类型