文章更新
在文章控制器中新增edit()方法
/**
* 修改文章
* @param int $id
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function edit(int $id, Request $request)
{
$article = Article::where('id', $id)->first();
// todo 验证文章是否存在,如果不存在跳转到错误提示页面
if ($request->post()) {
// todo 数据验证
Article::edit($article, $request->input());
// todo 判断是否修改成功
return $this->response->responseJSON();
}
// 分类信息,用于选择分类,因为在添加页面有一个选择分类。
$category = Category::getCategoryChildrenIdsByParentId(0);
$data['category'] = $category;
$data['article'] = $article;
return view('admin.article.edit', $data);
}
因为是编辑嘛,肯定要读数据了,如果不数据不存在,就跳转到错误页面,如果存在就展示编辑页面,在这分类选择那里要主要,因为我们数据表中已经有了选择的分类ID,在编辑的时候,分类应该直接被选中,不能在重新选了,比如我选择的是技术新闻分类,打开编辑的时候,技术新闻这个分类必须是选中的。
提交编辑信息的时候,应该做数据验证,编辑成果或者失败,应该返回给前端
如果接口报错,前端应该处理请求异常的情况。
完。