项目作者: mavinoo

项目描述 :
insert batch and update batch in laravel
高级语言: PHP
项目地址: git://github.com/mavinoo/laravelBatch.git
创建时间: 2017-10-07T10:41:01Z
项目社区:https://github.com/mavinoo/laravelBatch

开源协议:MIT License

下载


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

  1. use App\Models\User;
  2. $userInstance = new User;
  3. $arrays = [
  4. [
  5. 'conditions' => ['id' => 1, 'status' => 'active'],
  6. 'columns' => [
  7. 'status' => 'invalid',
  8. 'nickname' => 'mohammad',
  9. ],
  10. ],
  11. [
  12. 'conditions' => ['id' => 2],
  13. 'columns' => [
  14. 'nickname' => 'mavinoo',
  15. 'name' => 'mohammad',
  16. ],
  17. ],
  18. [
  19. 'conditions' => ['id' => 3],
  20. 'columns' => [
  21. 'nickname' => 'ali',
  22. ],
  23. ],
  24. ];
  25. $keyName = 'id';
  26. Batch::updateMultipleCondition($userInstance, $arrays, $keyName);
  27. // or
  28. batch()->updateMultipleCondition($userInstance, $arrays, $keyName);

Example Update 2

  1. use App\Models\User;
  2. $userInstance = new User;
  3. $value = [
  4. [
  5. 'id' => 1,
  6. 'status' => 'active',
  7. 'nickname' => 'Mohammad',
  8. ],
  9. [
  10. 'id' => 5,
  11. 'status' => 'deactive',
  12. 'nickname' => 'Ghanbari',
  13. ],
  14. ];
  15. $index = 'id';
  16. Batch::update($userInstance, $value, $index);
  17. // or
  18. batch()->update($userInstance, $values, $index);

Example Update 3

  1. use App\Models\User;
  2. $userInstance = new User;
  3. $value = [
  4. [
  5. 'id' => 1,
  6. 'status' => 'active',
  7. ],
  8. [
  9. 'id' => 5,
  10. 'status' => 'deactive',
  11. 'nickname' => 'Ghanbari',
  12. ],
  13. [
  14. 'id' => 10,
  15. 'status' => 'active',
  16. 'date' => Carbon::now(),
  17. ],
  18. [
  19. 'id' => 11,
  20. 'username' => 'mavinoo',
  21. ],
  22. ];
  23. $index = 'id';
  24. Batch::update($userInstance, $value, $index);
  25. // or
  26. batch()->update($userInstance, $values, $index);

Example Increment / Decrement

  1. use App\Models\User;
  2. $userInstance = new User;
  3. $value = [
  4. [
  5. 'id' => 1,
  6. 'balance' => ['+', 500], // Add
  7. ],
  8. [
  9. 'id' => 2,
  10. 'balance' => ['-', 200], // Subtract
  11. ],
  12. [
  13. 'id' => 3,
  14. 'balance' => ['*', 5], // Multiply
  15. ],
  16. [
  17. 'id' => 4,
  18. 'balance' => ['/', 2], // Divide
  19. ],
  20. [
  21. 'id' => 5,
  22. 'balance' => ['%', 2], // Modulo
  23. ],
  24. ];
  25. $index = 'id';
  26. Batch::update($userInstance, $value, $index);
  27. // or
  28. batch()->update($userInstance, $values, $index);

Example Insert

  1. use App\Models\User;
  2. $userInstance = new User;
  3. $columns = [
  4. 'firstName',
  5. 'lastName',
  6. 'email',
  7. 'isActive',
  8. 'status',
  9. ];
  10. $values = [
  11. [
  12. 'Mohammad',
  13. 'Ghanbari',
  14. 'emailSample_1@gmail.com',
  15. '1',
  16. '0',
  17. ],
  18. [
  19. 'Saeed',
  20. 'Mohammadi',
  21. 'emailSample_2@gmail.com',
  22. '1',
  23. '0',
  24. ],
  25. [
  26. 'Avin',
  27. 'Ghanbari',
  28. 'emailSample_3@gmail.com',
  29. '1',
  30. '0',
  31. ],
  32. ];
  33. $batchSize = 500; // insert 500 (default), 100 minimum rows in one query
  34. $result = Batch::insert($userInstance, $columns, $values, $batchSize);
  35. // or
  36. $result = batch()->insert($userInstance, $values, $index);
  1. // result: false or array
  2. sample array result:
  3. Array
  4. (
  5. [totalRows] => 384
  6. [totalBatch] => 500
  7. [totalQuery] => 1
  8. )

Example called from model

Add HasBatch trait into model:

  1. namespace App\Models;
  2. use Mavinoo\Batch\Traits\HasBatch;
  3. class User extends Model
  4. {
  5. use HasBatch;
  6. }

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

  1. use App\Models\User;
  2. // ex: update
  3. User::batchUpdate($value, $index);
  4. // ex: insert
  5. User::batchInsert($columns, $values, $batchSize);

Helper batch()

  1. // ex: update
  2. $result = batch()->update($userInstance, $value, $index);
  3. // ex: insert
  4. $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