目前,我的控制器将CRUD操作委托给CRUDService。
例如,当我需要更新资源时,该请求首先通过验证器,然后传递到CRUDService。如果这个程序发现资源不存在,它将返回404。
下面是执行该操作的代码片段:public function update($id, Request $input)
{
if ($this->notFound($id)) {
return $this->response->notFound();
}
$input = $this->converter->convert($input);
$this->model->where("id", $id)->update($input);
return $this->response->updated();
}
private function notFound($id)
{
return !count($this->model->find($id));
}
我想知道是否有办法在FormRequest类中执行此检查…有什么建议吗?
有。查看exists
Laravel验证规则:https://laravel.com/docs/5.3/validation规则存在
:
表,列要验证的字段必须存在于给定的数据库表中。
Exists规则基本用法
"状态"=比;的存在:各州的