是否有一种方法来检查被更新/删除的资源是否存在于验证器中?Laravel


Is there a way to check if the resource being updated/deleted exists in the validator? Laravel

目前,我的控制器将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规则基本用法

"状态"=比;的存在:各州的