Laravel Array Length Validation
Working with arrays in Laravel is very common, especially when you handle multiple input values like employees, tags, roles, categories, or products. In many situations, you need to validate how many items the array should contain. Laravel makes this very easy using built-in validation rules.
In this guide, you’ll learn how to validate the size of an array using min, max, between, and size rules. We’ll look at simple examples so you can understand how each rule works and apply them in your project.
Why Array Length Validation Matters
Imagine you are building a form that allows users to submit a list of employees. You may not want them to submit only one record or submit too many. Laravel’s validator helps you enforce:
- Minimum number of array elements
- Maximum number of array elements
- Exact size requirements
- A range of allowed sizes
Instead of manually checking array counts in PHP, you can let Laravel handle the validation logic.
Basic Array Rule
Before applying length constraints, you should tell Laravel that the input must be an array.
Example:
'employee' => 'array'
If the frontend sends something that is not an array, Laravel will throw a validation error automatically.
Validate Minimum Array Length
To ensure the array contains at least N elements, use the min rule.
Example: Require at least 2 employees
'employee' => 'array|min:2'
This is useful when a form requires multiple values, and you want to prevent submissions with a single item.
Validate Maximum Array Length
To limit the number of elements in the array, use the max rule.
Example: Allow maximum of 2 employees
'employee' => 'array|max:2'
If the user provides more than 2 items, the validation will fail.
Validate Range of Array Length
If you want to make sure your array has a minimum and maximum number of elements, use the between rule.
Example: Accept between 2 and 8 employees
'employee' => 'array|between:2,8'
This is a more flexible approach, because it covers both minimum and maximum in one rule.
Full Practical Example
Below is a real Laravel controller method that validates an incoming request. We use different validation options to show how each rule works. Adjust them according to your needs.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
class EmployeeController extends Controller
{
public function store(Request $request)
{
$request->validate([
// require at least 2 items
'employee' => 'array|min:2',
// OR require at most 2 items
// 'employee' => 'array|max:2',
// OR allow an array size between 2 and 8
// 'employee' => 'array|between:2,8',
]);
// your logic here...
}
}
Choose only one of these rules based on your requirement.
You should not use all three at the same time because the last rule will override the previous one.
Final Thoughts
Laravel’s validation system is powerful and clean. Instead of writing manual checks like count($array) < 2, you simply add validation rules to your form requests or controllers. The min, max, between, and size rules make it easy to control the number of elements an array should contain.