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

sur*_*amy 9 php arrays validation enums laravel-5.4

控制器功能:

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);
}
Run Code Online (Sandbox Code Playgroud)

这是插入图像数组的功能.为此,我使用了两个模型.插入的图像数组但是基于验证,应分别插入枚举值.我的验证是图像是必需的,最大尺寸及其扩展

Ale*_*ruk 3

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

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);
}
Run Code Online (Sandbox Code Playgroud)