ItSolutionStuff.com

Laravel Validation for Multiple Files in Array Example

By Hardik Savani • April 16, 2024
PHP Laravel

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, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Migration Add Column After Column Example

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

Get Array of Ids from Eloquent Models in Laravel

Read Now →

How to Create Migration in Laravel 9?

Read Now →

How to Add Two Factor Authentication with SMS in Laravel?

Read Now →

How to Change Column Length using Laravel Migration?

Read Now →

Laravel Include Blade File with Data Example

Read Now →

Laravel Send SMS to Mobile with Nexmo Example

Read Now →

Laravel Eloquent exists() and doesntExist() Example

Read Now →

Laravel Ajax Crop Image Before Upload using Croppie JS

Read Now →

Laravel Eloquent Group By with Multiple Columns Example

Read Now →

Laravel Storage Dropbox Integration Example

Read Now →

Laravel Image Resize & Upload with Intervention Image Example

Read Now →

Laravel File Upload with Validation Example

Read Now →