mavinoo/laravel-batch

在Laravel中批量插入和更新

v2.4.1 2024-09-17 11:09 UTC

README

在Laravel中批量插入和更新

License Latest Stable Version Total Downloads Daily Downloads

安装

composer require mavinoo/laravel-batch

服务提供者

在文件 app.php 中的 providers 数组中

Mavinoo\Batch\BatchServiceProvider::class,

别名

在文件 app.php 中的 aliases 数组中

'Batch' => Mavinoo\Batch\BatchFacade::class,

示例:更新多个条件

use App\Models\User;

$userInstance = new User;
$arrays = [
    [
        'conditions' => ['id' => 1, 'status' => 'active'],
        'columns'    => [
            'status' => 'invalid',
            'nickname' => 'mohammad',
        ],
    ],
    [
        'conditions' => ['id' => 2],
        'columns'    => [
            'nickname' => 'mavinoo',
            'name' => 'mohammad',
        ],
    ],
    [
        'conditions' => ['id' => 3],
        'columns'    => [
            'nickname' => 'ali',
        ],
    ],
];
$keyName = 'id';

Batch::updateMultipleCondition($userInstance, $arrays, $keyName);
// or
batch()->updateMultipleCondition($userInstance, $arrays, $keyName);

示例更新2

use App\Models\User;

$userInstance = new User;
$value = [
    [
        'id' => 1,
        'status' => 'active',
        'nickname' => 'Mohammad',
    ],
    [
        'id' => 5,
        'status' => 'deactive',
        'nickname' => 'Ghanbari',
    ],
];
$index = 'id';

Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);

示例更新3

use App\Models\User;

$userInstance = new User;
$value = [
    [
        'id' => 1,
        'status' => 'active',
    ],
    [
        'id' => 5,
        'status' => 'deactive',
        'nickname' => 'Ghanbari',
    ],
    [
        'id' => 10,
        'status' => 'active',
        'date' => Carbon::now(),
    ],
    [
        'id' => 11,
        'username' => 'mavinoo',
    ],
];
$index = 'id';

Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);

示例增减

use App\Models\User;

$userInstance = new User;
$value = [
    [
        'id' => 1,
        'balance' => ['+', 500], // Add
    ],
    [
        'id' => 2,
        'balance' => ['-', 200], // Subtract
    ],
    [
        'id' => 3,
        'balance' => ['*', 5], // Multiply
    ],
    [
        'id' => 4,
        'balance' => ['/', 2], // Divide
    ],
    [
        'id' => 5,
        'balance' => ['%', 2], // Modulo
    ],
];
$index = 'id';

Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);

示例插入

use App\Models\User;

$userInstance = new User;
$columns = [
    'firstName',
    'lastName',
    'email',
    'isActive',
    'status',
];
$values = [
    [
        'Mohammad',
        'Ghanbari',
        'emailSample_1@gmail.com',
        '1',
        '0',
    ],
    [
        'Saeed',
        'Mohammadi',
        'emailSample_2@gmail.com',
        '1',
        '0',
    ],
    [
        'Avin',
        'Ghanbari',
        'emailSample_3@gmail.com',
        '1',
        '0',
    ],
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query

$result = Batch::insert($userInstance, $columns, $values, $batchSize);
// or
$result = batch()->insert($userInstance, $values, $index);
// result: false or array

sample array result:
Array
(
    [totalRows]  => 384
    [totalBatch] => 500
    [totalQuery] => 1
)

从模型中调用示例

HasBatch 特性添加到模型中

namespace App\Models;

use Mavinoo\Batch\Traits\HasBatch;

class User extends Model
{
    use HasBatch;
}

然后从模型中调用 batchUpdate()batchInsert()

use App\Models\User;

// ex: update
User::batchUpdate($value, $index);

// ex: insert
User::batchInsert($columns, $values, $batchSize);

辅助批量()函数

// ex: update
$result = batch()->update($userInstance, $value, $index);


// ex: insert
$result = batch()->insert($userInstance, $columns, $values, $batchSize);

测试

如果你的项目中还没有安装phpunit,首先运行 composer require phpunit/phpunit

在Laravel应用的根目录中,运行 ./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests

捐赠

USDT地址:0x98410956169cdd00a43fe895303bdca096f37062