goldenboy1991 / laravel-batch
在 Laravel 中批量插入和更新数据
v1.0
2018-02-01 08:01 UTC
This package is not auto-updated.
Last update: 2024-09-22 09:13:33 UTC
README
在 Laravel 中批量插入和更新数据
安装
composer require goldenboy1991/laravel-batch:dev-master
服务提供者
在 app.php 文件中的 providers 数组中
Goldenboy\LaravelBatch\LaravelBatchServiceProvider::class,
别名
在 app.php 文件中的 aliases 数组中
'Batch' => Goldenboy\LaravelBatch\LaravelBatchFacade::class,
示例更新 1
$table = 'users';
$value = [
[
'id' => 1,
'status' => 'active',
'nickname' => 'Mohammad'
] ,
[
'id' => 5,
'status' => 'deactive',
'nickname' => 'Ghanbari'
] ,
];
$index = 'id';
Batch::update($table, $value, $index);
示例更新 2
$table = 'users';
$value = [
[
'id' => 1,
'status' => 'active'
],
[
'id' => 5,
'status' => 'deactive',
'nickname' => 'Ghanbari'
],
[
'id' => 10,
'status' => 'active',
'date' => Carbon::now()
],
[
'id' => 11,
'username' => 'goldenboy'
]
];
$index = 'id';
Batch::update($table, $value, $index);
示例插入
$table = 'users';
$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($table, $columns, $values, $batchSize);
// result : false or array
sample array result:
Array
(
[totalRows] => 384
[totalBatch] => 500
[totalQuery] => 1
)