美文网首页
Guarding Attributes 保护属性

Guarding Attributes 保护属性

作者: fireinme | 来源:发表于2017-12-01 16:53 被阅读0次

今天在对文章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();

成功

相关文章

网友评论

      本文标题:Guarding Attributes 保护属性

      本文链接:https://www.haomeiwen.com/subject/bcuwbxtx.html