承接 mavinoo/laravel-batch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mavinoo/laravel-batch

Composer 安装命令:

composer require mavinoo/laravel-batch

包简介

Insert and update batch (bulk) in laravel

README 文档

README

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 Multiple Condition

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

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

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

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);
// or
batch()->update($userInstance, $values, $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);
// or
$result = batch()->insert($userInstance, $values, $index);
// result: false or array

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

Example called from model

Add HasBatch trait into model:

namespace App\Models;

use Mavinoo\Batch\Traits\HasBatch;

class User extends Model
{
    use HasBatch;
}

And call batchUpdate() or batchInsert() from model:

use App\Models\User;

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

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

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

USDT Address: 0x98410956169cdd00a43fe895303bdca096f37062

mavinoo/laravel-batch 适用场景与选型建议

mavinoo/laravel-batch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.87M 次下载、GitHub Stars 达 594, 最近一次更新时间为 2018 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mavinoo/laravel-batch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mavinoo/laravel-batch 我们能提供哪些服务?
定制开发 / 二次开发

基于 mavinoo/laravel-batch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.87M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 602
  • 点击次数: 19
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 594
  • Watchers: 5
  • Forks: 124
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-03