Guarding Attributes 保护属性

2017-12-01  本文已影响0人  fireinme

今天在对文章topic模型进行插入数据时,总是提示
[图片上传中...(SqDfaYH8jO.png-d741a5-1512117930868-0)]
‘user_id’字段没有默认值,不能为空
然后改为加入user_id的值

$data = $request->all();
$data['user_id'] = Auth::user()->id;
$topic = Topic::create($data);

但是还是提示报错,后来发现是Topic模型,$fillable属性中没有添加‘user_id’,不允许批量赋值,
用create方法插入是批量赋值,并且以下这种方式也不行

$data = $request->all()
$topic = new Topic();
$topic->fill($data);
$topic->save();

最后改为

$topic = new Topic();
$topic->fill(\request()->all());
$topic->user_id = Auth::user()->id;
$topic->excerpt = '';
 $topic->save();

成功

上一篇下一篇

猜你喜欢

热点阅读