Laravel Array Length Validation
Today I will give you an Example of Laravel Array Length Validation. In this tutorial, I will show you Laravel validation array length. you will learn Laravel array length validation. This article will give you a simple example of checking array length in Laravel.
For most projects, we require validation for array minimum length or maximum length in Laravel. Laravel provides default validation rules for array validation. In this tutorial, we can use array, min, max, between, and size rules to apply to the array. let’s discuss Laravel Array Length Validation.
Here I am explaining an easy way to Laravel Array Length Validation with an example.
Array Min
When you have to validate that an array contains at least two employees, you can apply the min rule:
'employee' => 'array|min:2'
Array Max
When you have to validate that an array contains more than two employees, you can apply the max rule:
'employee' => 'array|max:2'
Array Between
When you have to validate that an array contains at least two, but not more than eight employees, then you can apply the between rule:
'employee' => 'array|between:2,8'
Full Example
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
class EmployeeController extends Controller
{
public function store(Request $request) {
$request->validate([
'employee' => 'array|min:2', //Array Min
'employee' => 'array|max:2', //Array Max
'employee' => 'array|between:2,8' //Array Between
]);
}
}