我想在Laravel中上传多个图像,错误显示文件名到刀片文件中。比如我想上传四个文件。其中两个是图像,另外两个是PDF文件,所以它应该阻止我这样做,因为我想只是图像上传,也会告诉我这两个PDF文件的名称是不允许的。但我得到了:
The uploads.0 must be an image.
The uploads.0 must be a file of type: jpeg, png, jpg, gif, svg.
The uploads.2 must be an image.
The uploads.2 must be a file of type: jpeg, png, jpg, gif, svg.
因此,我希望用户必须知道,到哪个文件,他们犯了错误,因为有可能上传超过20个文件,所以如果我可以打印文件名,他们可以很容易地确定哪里是问题。此外,我不能得到这个错误被打印下来的输入;我在Blade文件中使用此代码时出现此错误,
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
因此,我希望验证应该与文件名,它应该显示在输入文件下面,因为到我的其他输入字段显示。
到我的其他输入字段我已经显示了错误与此为所有错误
@if($errors->has('shipping_code'))
<div class="error text-danger fs-6 fw-400">{{ $errors->first('shipping_code') }}</div>
@endif
进入我的控制器
public function store(Request $request) {
$validator = Validator::make($request->all(),[
'shipping_code' => 'required|string',
'customer_name' => 'required|exists:users,id',
'shipper_name' => 'nullable|exists:users,id',
'shipping_details' => 'nullable|string',
'pickup_date' => 'required|date',
'delivery_date' => 'nullable|date',
'driver_name' => 'required|exists:users,id',
'driver_fee' => 'required|regex:/^\d+(\.\d{1,2})?$/',
'transaction_fee' => 'required|regex:/^\d+(\.\d{1,2})?$/',
'remarks' => 'nullable|string',
'status' => 'required|in:0,1,2,3',
'uploads' => 'required',
'uploads.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if ($validator->fails()) {
return redirect()->route('shipping.create')
->withErrors($validator)
->withInput();
}
else{
DB::transaction(function () use($request) {
$exPd = explode(',',$request->driver_name);
$driver_id = $exPd[0];
$shipping = new Shipping();
$shipping->shipping_code = $request->shipping_code;
$shipping->customer_id = $request->customer_name;
$shipping->shipper_id = $request->shipper_name;
$shipping->driver_id = $driver_id;
$shipping->shipping_details = $request->shipping_details;
$shipping->pickup_date = convertDateToDB($request->pickup_date);
$shipping->delivery_date = convertDateToDB($request->delivery_date);
$shipping->driver_fee = $request->driver_fee;
$shipping->transaction_amount = $request->transaction_fee;
$shipping->remarks = $request->remarks;
$shipping->status = $request->status;
$shipping->created_at = date('Y-m-d H:i:s');
$shipping->created_by = Auth::user()->id;
$shipping->save();
if($request->hasfile('uploads')) {
foreach($request->file('uploads') as $file)
{
$input['imagename'] = time().'.'.$file->extension();
$filePath = public_path('/uploads/shipping_transaction_images/');
$img = Image::make($file->path());
$img->resize(110, 110, function ($const) {
$const->aspectRatio();
})->save($filePath.$input['imagename']);
$imgData[] = $input['imagename'];
$fileModal = new ShippingImage();
$fileModal->shipping_id = $shipping->id;
$fileModal->image_type = 'pickup';
$fileModal->image_file = $input['imagename'];
$fileModal->save();
}
}
});
return redirect()->route('shippings.index')
->with('success','Shipping Transaction Created Successfully');
}
}
2条答案
按热度按时间mwg9r5ms1#
你可以这样做你的验证:
您可以获取其他字段的错误,如您使用的方式:
但是对于上传,您可以使用。
如果未选择文件,则用于获取:
如果多个选定的文件有错误:
它也不会伤害你的
DB::transaction
。参见 * 获取laravel中失败的多个上传文件的文件名 *。0qx6xfy62#