【发布时间】:2017-09-17 02:15:38
【问题描述】:
我正在使用 Lumen (5.4) 做我的第一步,我想将一个数据数组存储到数据库中。所以,我有:
MySQL rate_plans 表
id PK AUTO_INCREMENT
name VARCHAR(45) NOT_NULL
slug VARCHAR(45) NOT_NULL
price FLOAT(5,2) NOT_NULL
created_at DATETIME NOT_NULL
updated_at DATETIME NOT_NULL
雄辩的模型
use Illuminate\Database\Eloquent\Model as Model;
class RatePlan extends Model {
protected $table = 'rate_plans';
protected $dateFormat = 'Y-m-d H:i:s';
protected $fillable = ['name', 'price'];
protected $guarded = [];
}
问题是,Eloquent 使用正确的 id、created_at 和 updated_at 属性值创建记录。但是,name,slug 是空的,价格是 0.00。代码是:
$data = ['name' => 'Test', price => 99.99];
$model = new \App\Models\RatePlan();
$model->fill($data);
$model->slug = 'test';
$model->status = 'active';
$model->create();
有什么想法吗?为什么 Eloquent 在持久化 null 值时不会失败?
【问题讨论】: