写了一个小方法,应该为数据库中的特定条目添加注释。
创建了一个包含以下字段的评论表: comment_id - 评论的 id author_id - 评论的辣椒的 id post_id - 评论的帖子的 id comment - 评论本身
方法本身:
public function store(Request $request)
{
$comment = new Comment();
$comment->comment = $request->comment;
$comment->author_id = \Auth::user()->id;
$comment->post_id = $request->post;
$comment->save();
return redirect()->back()->with('success', 'Комментарий добавлен');
}
我希望将正在评论的帖子的 ID 保存到数据库中,我从获取请求中获取帖子的 ID。我进入我的刀片模板并在那里写{{ request()->post }},结果,所需记录的 id 显示在屏幕上,我尝试将此 id 保存为字符串$comment->post_id = $request->post;,结果我收到 post_id 为 null 的错误Column 'post_id' cannot be null
我怎样才能做到正确?
尝试
$request->input('post');如果我正确理解了所有内容,那么在模板中表单的 POST 请求中,此行不会出现
如果是这样,那么解决方案 1. 在刀片模板中 - 将包含您的数据的隐藏输入字段添加到提交数据的表单( request()->post ):
在控制器的 store 方法中,从 $request 中获取这个数据
$comment->post_id = ($request->has('post_id')) ?$request->get('post_id') : null;