我正在尝试创建一个新的模型对象:$ts = Carbon::create(2020, 04, 16, 13, 00, 0, null);$m1 = new Measure(['time' => $ts]);dd($m1);但是当我检查结果为$m1->time空时这是我的模型配置:class Measure extends Model{ protected $table = 'measures'; protected $casts = [ 'time' => 'datetime' ]; public $timestamps = [ "time" ]; public $guarded = [ 'id' ];}如果我做:$m1 = new Measure();$m1->time = $ts;我将能够看到$m1->time价值为什么会这样?
1 回答
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
time需要在数组中指定字段$fillables,以便可以批量分配(在创建新模型时从数组)。
因此在模型中,可填充数组需要像这样定义:
class Measure extends Model
{
protected $table = 'measures';
protected $casts = [
'time' => 'datetime'
];
public $timestamps = [
"time"
];
public $guarded = [
'id'
];
//**** fillables array *****//
protected $fillable = ['time'];
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消