gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:20:55 24 4
gpt4 key购买 nike

Controller 函数:

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

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

最佳答案

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

关于php - 如何验证 laravel 中插入的图像数组并需要根据验证插入枚举值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com