Laravel 内批量更新数据

推荐个好用的批量更新包 laravelBatch#

GitHub 地址 https://github.com/mavinoo/laravelBatch

除了 json 字段需要特殊注意下,其他字段更新用起来都很愉快

json 字段: 如果批量内也有 json string 带有 \n 这种 在处理时候需要特殊注意下 需要替换为 \\n

否则入库会提示: Invalid Data

Laravel BATCH (BULK)#

Insert and update batch (bulk) in laravel

License
Latest Stable Version
Total Downloads
Daily Downloads

Install#

composer require mavinoo/laravel-batch

Service Provider#

file app.php in array providers :

Mavinoo\Batch\BatchServiceProvider::class,

Aliases#

file app.php in array aliases :

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

Example Update 1#

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);

Example Update 2#

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);

Example Increment / Decrement#

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);

Example Insert#

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);
// result : false or array

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

Helper batch()#

// ex: update

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


// ex: insert

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

Tests#

If you don’t have phpunit installed on your project, first run composer require phpunit/phpunit

In the root of your laravel app, run ./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests

Donate#

BTC Address: 16XDxkcqiEJ8DYf4xWxu7WK3Peo29TvXxD

本作品采用《CC 协议》,转载必须注明作者和本文链接
每天一点小知识,到那都是大佬,哈哈
本帖由 MArtian 于 3年前 加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 23

最近有这方面的需求,先 mark 一下

3年前 评论
raybon (楼主) 3年前

这还挺好, 之前都是拼接原生 sql 用 case when 更新,这样写实在是不方便。

3年前 评论

updateOrInsert 方法吗?

3年前 评论
raybon (楼主) 3年前

您好,我在使用 Batch::update($userInstance, $value, $index); 方法更新的时候总是报错提示 Non-static method Mavinoo\\Batch\\Batch::update() cannot be called statically。您又遇到这种情况吗

3年前 评论
raybon (楼主) 3年前
Mars1499593644 3年前
看上隔壁小花了啦 3年前
raybon (楼主) 3年前
tiantian10000 2年前
raybon (楼主) 2年前
福寿绵绵 1年前
hardshen

支持自定义的 case when 参数吗

2年前 评论
raybon (楼主) 2年前

请问可以支持多个字段查询更新吗?

2年前 评论
raybon (楼主) 2年前
makeGao (作者) 2年前

Using $this when not in object context
大佬,使用报这个错误,是不是静态代理类没找到

1年前 评论
raybon (楼主) 1年前
15001002938 1年前
raybon (楼主) 1年前