簡體   English   中英

Laravel使用數據庫列通過特征設置$ fillable

[英]Laravel set $fillable via trait using database columns

我正在嘗試創建一個特征,該特征將基於模型表模式自動設置$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受保護。

這可能嗎? 如果是這樣,怎么辦?

我只是想知道,如果您打算使表上的所有字段都可填充,為什么會在為其創建代碼時遇到問題?

你能放嗎:

protected $guarded = [];

在你的模特上?

只是一個想法。

我想我明白了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM