如何验证 laravel 中插入的图像数组并需要根据验证插入枚举值?

How to validate array of images inserted in laravel and need to insert the enum value based on validation?

控制器功能:

public function addImages(Request $request,$imagesProductId)
{
    $product   = Product::create($request->all());
    $filenames = array();

    if ($request->images == '') {
        return Redirect::back()->withErrors(['msg', 'The Message']);
    }

    if () {
    // also need to validate on the extension and resolution of images 
    // (ie., if the validation fails the enum value will be "QCFailed")
    } else {
        foreach ($request->images as $photo) {
            $filename    = substr($photo->store('public/uploadedImages'), 22);
            $filenames[] = asset('storage/uploadedImages/'.$filename);

            ProductsPhoto::create([
                'product_id'    => $product->id,
                'productId'     => $imagesProductId,
                'nonliveStatus' =>"QCVerified",   
                'filename'      => $filename
            ]);
        }

        // echo('nonliveStatus');
    }

    return response()->json($filenames);
}

这是我的函数,用于插入 images.For 数组,我使用了两个 model.The 图像数组,但基于验证,应该分别插入枚举值。我的验证是图像是必需的,最大尺寸及其扩展

根据Laravel 5.4 documentation,您需要使用一组规则创建验证器对象。像这样:

public function addImages(Request $request, $imagesProductId)
{
    $product   = Product::create($request->all());
    $filenames = array();

    if (empty($request->images)) {
        return Redirect::back()->withErrors(['msg', 'The Message']);
    }

    $rules = [
        'images' => 'mimes:jpeg,jpg,png'                 // allowed MIMEs
            . '|max:1000'                                // max size in Kb
            . '|dimensions:min_width=100,min_height=200' // size in pixels
    ];

    $validator = Validator::make($request->all(), $rules);
    $result    = $validator->fails() ? 'QCFailed' : 'QCVerified';

    foreach ($request->images as $photo) {
        $filename    = substr($photo->store('public/uploadedImages'), 22);
        $filenames[] = asset('storage/uploadedImages/'.$filename);

        ProductsPhoto::create([
            'product_id'    => $product->id,
            'productId'     => $imagesProductId,
            'nonliveStatus' => $result,
            'filename'      => $filename
        ]);
    }

    return response()->json($filenames);
}

最近我在 laravel 6 中使用 Dropzone 进行多张图片上传,但在验证一组图片时遇到了问题, 最后,它解决了我的两步验证。 一个用于整个数组,第二个用于数组的每个元素。 我把代码放在这里,也许对某人有帮助。

        $request->validate([
        'product_code'  =>  'required|string',
        'product_name'  =>  'required|string',
        'product_price' =>  'required|string',
        'product_count' =>  'required|string',
        'category_id'   =>  'nullable|numeric',
        'file'          =>  'nullable',    //name of image field in form
        'file.*'          =>  'max:2048|image' //name of image field in form
    ]);