定制 dazza-dev/laravel-batch-validation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dazza-dev/laravel-batch-validation

Composer 安装命令:

composer require dazza-dev/laravel-batch-validation

包简介

Batch validate arrays in Laravel and optimize database unique rules using whereIn to avoid N+1 query issues.

README 文档

README

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.

统计信息

  • 总下载量: 965
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固