常规错误:1364 字段“user_id”没有默认值
我正在尝试为当前用户分配user_id,但它给我这个错误
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a
default value (SQL: insert into `posts` (`updated_at`, `created_at`)
values (2017-04-27 10:29:59, 2017-04-27 10:29:59))
这是我的方法
//PostController
Post::create(request([
'body' => request('body'),
'title' => request('title'),
'user_id' => auth()->id()
]));
用这些填充物
//Post Model
protected $fillable = ['title', 'body', 'user_id']
但是,此方法可以完成工作
//PostController
auth()->user()->publish(new Post(request(['title' ,'body'])));
//Post Model
public function publish(Post $post)
{
$this->posts()->save($post);
}