Skip to content

insert batch and update batch in laravel

License

Notifications You must be signed in to change notification settings

s21825/laravelBatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Laravel BATCH (BULK)

Insert and update batch (bulk) in laravel

Install

composer require mavinoo/laravel-batch:2.0

NOTE: Click to install the previous version 1.0.

Service Provider

file app.php in array providers :

Mavinoo\LaravelBatch\LaravelBatchServiceProvider::class,

Aliases

file app.php in array aliases :

'Batch' => Mavinoo\LaravelBatch\LaravelBatchFacade::class,

Example Update 1

$table = 'users';

$value = [
     [
         'id' => 1,
         'status' => 'active',
         'nickname' => 'Mohammad'
     ] ,
     [
         'id' => 5,
         'status' => 'deactive',
         'nickname' => 'Ghanbari'
     ] ,
];

$index = 'id';


Batch::update($table, $value, $index);

Example Update 2

$table = 'users';

$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($table, $value, $index);

Example Insert

$table = 'users';

$columns = [
     'firstName',
     'lastName',
     'email',
     'isActive',
     'status',
];

$values = [
     [
         'Mohammad',
         'Ghanbari',
         '[email protected]',
         '1',
         '0',
     ] ,
     [
         'Saeed',
         'Mohammadi',
         '[email protected]',
         '1',
         '0',
     ] ,
     [
         'Avin',
         'Ghanbari',
         '[email protected]',
         '1',
         '0',
     ] ,
];

$batchSize = 500; // insert 500 (default), 100 minimum rows in one query

$result = Batch::insert($table, $columns, $values, $batchSize);
// result : false or array

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

About

insert batch and update batch in laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%