Laravel validation for multiple files in array
In this post we will give you information about Laravel validation for multiple files in array. Hear we will give you detail about Laravel validation for multiple files in arrayAnd how to use it also give you demo for it if it is necessary.
Hi guys, in this post, we will learn how to add multiple file upload validation with array in laravel 5.7. we almost require for multiple images or file upload, so you also need to use validation like required, mimes, max etc. here you will see validation for multiple images in laravel 5.
I can say you when you require to use multiple file validation like when you are doing multiple image upload and you have array of images or file object on post request then you can use this validation. so let’s see bellow two way to validation in laravel.
Way 1:
public function store(Request $request)
{
$this->validate($request, [
'images.*' => 'required|mimes:jpg,jpeg,png,bmp|max:2000'
],[
'images.*.required' => 'Please upload an image only',
'images.*.mimes' => 'Only jpeg, png, jpg and bmp images are allowed',
'images.*.max' => 'Sorry! Maximum allowed size for an image is 2MB',
]);
// Write your code
}
Way 2:
public function store(Request $request)
{
$input = $request->all();
$validator = Validator::make(
$input,
[
'images.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'images.*.required' => 'Please upload an image',
'images.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'images.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);
if ($validator->fails()) {
// If fails then return Validation error..
}
// Write your code
}
I hope it can help you…
Hope this code and post will helped you for implement Laravel validation for multiple files in array. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs