ItSolutionStuff.com

Laravel Array Length Validation Example

By Hardik Savani β€’ November 5, 2023
Laravel

Hey Dev,

In this short guide, we will show you laravel validation array length. you will learn laravel array length validation. let’s discuss about validate array length laravel. This article will give you a simple example of check array length laravel. Alright, let’s dive into the steps.

Sometimes, we require to add validation for array minimum length or maximum length in laravel. user must need to add at least two array value. so Laravel provides default validation rules for array validation. we can use array, min, max, between and size rules to apply for array.

You can see the simple solution with the controller code:

Solution:

Laravel Validation Array Min:

When you have to validate that an array contains at least three users, you can apply the min rule:

'users' => 'array|min:3'

Laravel Validation Array Max:

When you have to validate that an array contains more then three users, you can apply the max rule:

'users' => 'array|max:3'

Laravel Validation Array Between:

When you have to validate that an array contains at least three, but not more than ten users, you can apply the between rule:

'users' => 'array|between:3,10'

Controller Code:

Now, you can see the controller code for example of validation:

FormController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Illuminate\View\View;

use Illuminate\Http\RedirectResponse;

class FormController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create(): View

{

return view('createUser');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(Request $request): RedirectResponse

{

$request->validate([

'users' => 'array|between:3,10'

]);

...

return back()->with('success', 'User created successfully.');

}

}

I hope it can help you...

Tags: Laravel
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 10 Custom Validation Rule Example

Read Now β†’
β˜…

Laravel Migration Add Column After Column Example

Read Now β†’
β˜…

Laravel Migration Default Value Current Timestamp Example

Read Now β†’
β˜…

How to Add Custom Attribute in Laravel Model?

Read Now β†’
β˜…

Get Array of Ids from Eloquent Models in Laravel

Read Now β†’
β˜…

How to Set Default Value in Laravel Model?

Read Now β†’
β˜…

How to Get All Models in Laravel?

Read Now β†’
β˜…

Laravel React JS Form Validation Example

Read Now β†’
β˜…

Laravel 9 Form Validation Tutorial Example

Read Now β†’
β˜…

How to Run Migration and Seeder on Laravel Vapor?

Read Now β†’
β˜…

How to Change Table Name using Laravel Migration?

Read Now β†’
β˜…

How to Change Column Name and Data Type in Laravel Migration?

Read Now β†’
β˜…

Laravel File Upload with Validation Example

Read Now β†’
β˜…

Laravel Create Custom Validation Rule Example

Read Now β†’