New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

dazza-dev/laravel-batch-validation

Batch validate arrays in Laravel and optimize database unique rules using whereIn to avoid N+1 query issues.
1,339 22
Install
composer require dazza-dev/laravel-batch-validation
Latest Version:v2.2.0
PHP:>=8.2
Laravel:11 · 12 · 13
Older releases:8 · 9 · 10
License:MIT
Last Updated:Jun 14, 2026
Links: GitHub  ·  Packagist
Maintainer: dazza-dev

Laravel Batch Validation

When performing array validations in Laravel with unique rules, Laravel validates one record at a time, causing an N+1 query problem. This is a common issue when importing data from an Excel file and validating it before inserting it into the database. This package solves the problem by batching the unique validations and using whereIn to prevent the N+1 problem.

Install

composer require dazza-dev/laravel-batch-validation

Usage

This package is easy to use. Here's an example of how to apply it:

use Illuminate\Support\Facades\Validator;

$data = [
    ['name' => 'User 1', 'email' => 'user1@example.com'],
    ['name' => 'User 2', 'email' => 'user2@example.com'],
    ['name' => 'User 3', 'email' => 'user3@example.com'],
];

// Validator Instance
$validator = Validator::make($data, [
    '*.name' => 'required',
    '*.email' => 'email:strict|unique:contacts,email',
]);

// Validate in Batches (this prevent n+1 problem)
$validator->validateInBatches();

// Validation fails
if ($validator->fails()) {
    throw new \Exception(json_encode($validator->errors()->messages()));
}

Batch Size

You can change the batch size by passing the batchSize parameter to the validateInBatches method. The default batch size is 10.

$validator->validateInBatches(batchSize: 20);

Database Rules

This package also supports database rules like unique, exists.

$validator = Validator::make($data, [
    '*.name' => 'required',
    '*.email' => 'email:strict|unique:contacts,email',
]);

Form Request

You can use the validateInBatches method in a form request.

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;

class StoreContactRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            '*.name' => 'required',
            '*.email' => 'email:strict|unique:contacts,email',
        ];
    }

    public function withValidator(Validator $validator)
    {
        if (method_exists($validator, 'validateInBatches')) {
            $validator->validateInBatches(batchSize: 100);
        }
    }
}

Before and After Optimization

Before (N+1 problem)

Before Optimization

After (Optimized)

After Optimization

Contributions

Contributions are welcome. If you find any bugs or have ideas for improvements, please open an issue or send a pull request. Make sure to follow the contribution guidelines.

Author

Laravel Batch Validation was created by DAZZA.

License

This project is licensed under the MIT License.

Related Packages

claraleigh/naughty-username

Validate that a username is not a naughty one

24,348 0
cohensive/validation

Extra functionality for Laravel 5 Validator.

13,938 14
jedrzej/validator-extended-syntax

Laravel 4/5 package that adds validator negation to Laravel's validation module

11,595 3
shin1x1/laravel-validator-builder

Laravel 4 Validator builder

7,436 2
digitonic/validation

A Laravel custom validation package with some out of the box validators.

2,526 2

Version History

Version Released PHP Laravel License
v2.2.0 >=8.2 ^11.0|^12.0|^13.0 MIT
v2.1.0 >=8.2 ^11.0|^12.0 MIT
v2.0.0 >=7.4 ^11.0 MIT
v1.0.0 >=7.4 ^8.0 || ^9.0 || ^10.0 MIT