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

时间:2023-01-26 15:02:09

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

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

1 个解决方案

#1


2  

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

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

#1


2  

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

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