php - 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
受到保护。
这可能吗?如果是这样,怎么做?
解决方案
我想我明白了:
trait DynamicFillable
{
public function getFillable()
{
return Schema::getColumnListing($this->getTable());
}
}
推荐阅读
- android-studio - Android 项目包含以“._”开头的额外文件
- html - 如何计算自定义 ValidityState
- c++ - How to group decimal places as well?
- hive - 为什么在 Hive 中映射器的数量变化如此之大以进行插入覆盖
- java - 无法解压缩 zip4j 库 java 创建的 zip 文件
- r - 如何修复R中的“替换有x行,数据有z”
- javafx - 为什么我的 CellFactory 中的第一个 TableRow 为空?
- php - 我从数据库中下载图像的功能不起作用
- html - 悬停时触发可翻转卡片的问题
- selenium - Chrome 无头不适用于重定向 (OAUTH2)