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

Viewed 1103

Controller function:

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);
}

This is myfunction to insert array of images.For that I have used two model.The array of images get inserted but based on the validation the enum value should inserted respectively..My validations are the images are required and max size and its extensions

2 Answers

According Laravel 5.4 documentation you need to create validator object with set of rules. Something like this:

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);
}

Recently I used Dropzone for multiple image upload in laravel 6 and get in problem to validate an array of images, finally, it solved my two-step validation. one for the whole array and second validation for each element of the array. I put the code here, maybe helpful for someone.

        $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
    ]);
Related