Devnote

Web Development Blog Company, Services India

MySQL Error 121 Duplicate key on write - Practical Repair Strategy
MySQL Error 121 Duplicate key on write – Practical Repair Strategy
February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Laravel Array Length Validation

Laravel Array Length Validation

Image
Devnote team
September 25, 2023
2
1.3K Views
0 Comments

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.

  1. Why Array Length Validation Matters
  2. Basic Array Rule
  3. Validate Minimum Array Length
  4. Validate Maximum Array Length
  5. Validate Range of Array Length
  6. Full Practical Example
  7. Final Thoughts

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.

Categories:

LaravelPHP

Other Articles

How to Redirect to Another Page After 5 Seconds using jQuery
Previous

How to Redirect to Another Page After 5 Seconds using jQuery?

Magic Navigation Menu using HTML CSS and Javascript
Next

Magic Navigation Menu using HTML CSS and Javascript

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

PHP cURL error 60: SSL certificate problem - Windows & Linux Fix

PHP cURL error 60: SSL certificate problem – Windows & Linux Fix

February 11, 2026
PHP 8.3 Fatal Error: Cannot use string offset as an array - Real World Case

PHP 8.3 Fatal Error: Cannot use string offset as an array – Real World Case

January 30, 2026
Image

How to Fix Nginx 413 Request Entity Too Large for Image & Video Uploads

January 28, 2026
Fix Laravel 11 Class App\Http\Controllers not found After Route Auto-Discovery

Fix Laravel 11 Class App\Http\Controllers not found After Route Auto-Discovery

January 19, 2026
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement