Laravel |验证数组内每个元素的唯一性

时间:2020-08-15 23:45:24

标签: laravel laravel-validation laravel-formrequest

用户可以将一系列图像上传到我的应用程序,而这些图像在我的数据库中已经不存在。

上载新文件时,我会存储文件的MD5哈希值,然后可以使用此值检查它是否已存在。

我使用自定义的FormRequest进行验证:

public function rules()
    {
        return [
            'pictures' => 'required|array|min:1',
            'pictures.*' => [
                'required',
                'image',
                'mimes:jpg,png,jpeg',
                'max:5120',
                Rule::unique('files')->where(function ($query) {
                    //How to get the current item of this array to check? 
                    //Is it a good idea?
                    //$query->where('hash', $currentImageHash )

                })
            ]
        ];
    }

0 个答案:

没有答案