2017 © Pedro Peláez
 

library laravel-batch

Insert and update batch (bulk) in laravel

image

mavinoo/laravel-batch

Insert and update batch (bulk) in laravel

  • Wednesday, April 25, 2018
  • by mavinoo
  • Repository
  • 3 Watchers
  • 37 Stars
  • 3,730 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 2 Versions
  • 185 % Grown

The README.md

Laravel BATCH (BULK)

Insert and update batch (bulk) in laravel, (*1)

License Latest Stable Version Total Downloads Daily Downloads, (*2)

Install

composer require mavinoo/laravel-batch, (*3)

Service Provider

File app.php in array providers:, (*4)

Mavinoo\Batch\BatchServiceProvider::class,, (*5)

Aliases

File app.php in array aliases:, (*6)

'Batch' => Mavinoo\Batch\BatchFacade::class,, (*7)

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:, (*8)

namespace App\Models;

use Mavinoo\Batch\Traits\HasBatch;

class User extends Model
{
    use HasBatch;
}

And call batchUpdate() or batchInsert() from model:, (*9)

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, (*10)

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

Donate

USDT Address: 0x98410956169cdd00a43fe895303bdca096f37062, (*12)

The Versions

25/04 2018

dev-master

9999999-dev

Insert and update batch (bulk) in laravel

  Sources   Download

MIT

01/02 2018

v1.0

1.0.0.0

Generates an update string based on the data you supply, and runs the query.

  Sources   Download

MIT