free3_5man/laravel-eloquent-batch
Composer 安装命令:
composer require free3_5man/laravel-eloquent-batch
包简介
a common-used batch util is provided for batch insert/update in multiple databases, also with laravel eloquent based batch action
README 文档
README
provide an util for batch Insert/update, also an eloquent collection macro batchSave for batch action more convenient in Laravel.
Install
composer require free3_5man/laravel-eloquent-batch
Service Provider
file app.php in array providers :
Freeman\LaravelBatch\BatchServiceProvider::class,
Features
- make the insert data items assoc with keys, which has the same data structure with update data
- return insert ids when primary key is integer auto_increment
- MySQL, PostgreSQL, SQLite and SQL Server is supported
- json/jsonb field is supported
- auto chunked when generate batch sql
- support Eloqunent Collection macro batchSave for auto batchInsert and batchUpdate
- batchSave fill items with insert id when actually insert and primary key is integer auto_increment
- batchSave return updated models when actually update
- batchSave can save inserts and updates at the same time
- batchSave can control whether to touch updated_at with the param $touchUpdatedAt
Batch Insert Example
use App\Models\Player; use Freeman\LaravelBatch\BatchUtil; $data = [ // this is the first item, columns of this item will be used as insert columns // if columns of any other items does not equal to this, false will be returned as result // all the columns of each item required to be same [ 'name' => 'kaka', 'birthday' => '1982-04-22', ], [ 'name' => 'nesta', 'birthday' => '1976-03-19', ], [ 'name' => 'pirlo', 'birthday' => '1979-05-19', ], ]; // batchInsert use the first item columns as insert columns default; $ids = BatchUtil::ofModel(new Player)->batchInsert($data); // if players primay key auto_increment, insert ids array ([1, 2, 3]) will be returned, else empty array ([]) will be returned $data[1]['is_captain'] = true; BatchUtil::ofModel(new Player)->batchInsert($data); // return false
Batch Update Example
$data = [ [ 'id' => 1, 'name' => 'kaka', 'birthday' => '1982-04-22', ], [ 'id' => 2, 'name' => 'nesta', 'birthday' => '1976-03-19', ], [ 'id' => 3, 'name' => 'pirlo', 'birthday' => '1979-05-19', ], ]; BatchUtil::ofModel(new Player)->batchUpdate($data);
batchSave Example
$data = [ [ 'name' => 'kaka', 'birthday' => '1982-04-22', ], [ 'name' => 'nesta', 'birthday' => '1976-03-19', ], [ 'name' => 'pirlo', 'birthday' => '1979-05-19', ], ]; // batchSave will do batch insert here $players = collect($data)->map(function($item) { return new Player($item); })->batchSave(); // each player has id dump($players->pluck('id')->toArray()); // [1, 2, 3] // batchSave will do batch update here, updated_at will be touched automaticly $players->map(function($player) { return $player->fill([ 'name' => $player->name . '-' . $player->id, ]); })->batchSave(); dump($players->pluck('name')->toArray()); // ['kaka-1', 'nesta-2', 'pirlo-3']
Testing
$ composer test
free3_5man/laravel-eloquent-batch 适用场景与选型建议
free3_5man/laravel-eloquent-batch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 free3_5man/laravel-eloquent-batch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 free3_5man/laravel-eloquent-batch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-30