首页 > 解决方案 > Laravel 使用数据库列通过 trait 设置 $fillable

问题描述

我正在尝试创建一个将$fillable根据模型表架构自动设置的特征:

trait DynamicFillable
{
    public static function bootDynamicFillableTrait()
    {
        static::retrieved(function (Model $model) {
            $model->fillable(Schema::getColumnListing($model->getTable()));
        });
    }
}

我也试过:

static::retrieved(function (Model $model) {
    $model->fillable = Schema::getColumnListing($model->getTable());
});

这也不起作用,因为$fillable受到保护。

这可能吗?如果是这样,怎么做?

标签: phplaraveltraits

解决方案


我想我明白了:

trait DynamicFillable
{
    public function getFillable()
    {
        return Schema::getColumnListing($this->getTable());
    }
}

推荐阅读