Laravel 使用数据库列通过特征设置 $fillable
Laravel set $fillable via trait using database columns
我正在尝试创建一个特征,该特征将根据模型 table 模式自动设置 $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
受到保护。
这可能吗?如果是,怎么做?
我想我明白了:
trait DynamicFillable
{
public function getFillable()
{
return Schema::getColumnListing($this->getTable());
}
}
我只是想知道,如果您打算让 table 上的所有字段都可填写,为什么 运行 会在为其创建代码时遇到问题?
你能不能把:
protected $guarded = [];
在你的模型上?
只是一个想法。
我正在尝试创建一个特征,该特征将根据模型 table 模式自动设置 $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
受到保护。
这可能吗?如果是,怎么做?
我想我明白了:
trait DynamicFillable
{
public function getFillable()
{
return Schema::getColumnListing($this->getTable());
}
}
我只是想知道,如果您打算让 table 上的所有字段都可填写,为什么 运行 会在为其创建代码时遇到问题?
你能不能把:
protected $guarded = [];
在你的模型上?
只是一个想法。