Commit 92990afb by 杨树贤

去除包

parent 6a65bded
...@@ -11,7 +11,6 @@ use App\Model\SupplierChannelModel; ...@@ -11,7 +11,6 @@ use App\Model\SupplierChannelModel;
use App\Model\SupplierExaminationModel; use App\Model\SupplierExaminationModel;
use App\Utils\CsvValueBinder; use App\Utils\CsvValueBinder;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Mavinoo\Batch\Batch;
class SupplierExaminationService class SupplierExaminationService
{ {
...@@ -221,7 +220,9 @@ class SupplierExaminationService ...@@ -221,7 +220,9 @@ class SupplierExaminationService
if ($updateData) { if ($updateData) {
$updateData = collect($updateData); $updateData = collect($updateData);
foreach ($updateData->chunk(50) as $chunk) { foreach ($updateData->chunk(50) as $chunk) {
Batch::update(new SupplierExaminationModel, $chunk->toArray(), 'id'); foreach ($chunk->toArray() as $item) {
SupplierExaminationModel::where('id', $item['id'])->update($item);
}
} }
} }
return true; return true;
......
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
"predis/predis": "^1.1", "predis/predis": "^1.1",
"vladimir-yuldashev/laravel-queue-rabbitmq": "5.2", "vladimir-yuldashev/laravel-queue-rabbitmq": "5.2",
"maatwebsite/excel": "2.1.0", "maatwebsite/excel": "2.1.0",
"guzzlehttp/guzzle": "6.3", "guzzlehttp/guzzle": "6.3"
"mavinoo/laravel-batch": "^2.3"
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "~1.4", "fzaninotto/faker": "~1.4",
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "d2d0e5b8611ab1b35f70773d220c455b", "content-hash": "5838f7b8ccfd9204e310d51188dd56a7",
"packages": [ "packages": [
{ {
"name": "classpreloader/classpreloader", "name": "classpreloader/classpreloader",
...@@ -900,68 +900,6 @@ ...@@ -900,68 +900,6 @@
"time": "2015-12-10T18:31:14+00:00" "time": "2015-12-10T18:31:14+00:00"
}, },
{ {
"name": "mavinoo/laravel-batch",
"version": "v2.3.4",
"source": {
"type": "git",
"url": "https://github.com/mavinoo/laravelBatch.git",
"reference": "126f077dec4ecabb67331694e71bf28b427daa60"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mavinoo/laravelBatch/zipball/126f077dec4ecabb67331694e71bf28b427daa60",
"reference": "126f077dec4ecabb67331694e71bf28b427daa60",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3@dev"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Mavinoo\\Batch\\BatchServiceProvider"
],
"aliases": {
"Batch": "Mavinoo\\Batch\\BatchFacade"
}
}
},
"autoload": {
"files": [
"src/Common/Helpers.php"
],
"psr-4": {
"Mavinoo\\Batch\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mohammad Ghanbari",
"email": "mavin.developer@gmail.com"
}
],
"description": "Insert and update batch (bulk) in laravel",
"support": {
"issues": "https://github.com/mavinoo/laravelBatch/issues",
"source": "https://github.com/mavinoo/laravelBatch/tree/v2.3.4"
},
"time": "2022-04-20T12:04:23+00:00"
},
{
"name": "monolog/monolog", "name": "monolog/monolog",
"version": "1.25.3", "version": "1.25.3",
"source": { "source": {
......
...@@ -156,7 +156,6 @@ return [ ...@@ -156,7 +156,6 @@ return [
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
Maatwebsite\Excel\ExcelServiceProvider::class, Maatwebsite\Excel\ExcelServiceProvider::class,
Mavinoo\Batch\BatchServiceProvider::class,
], ],
...@@ -203,7 +202,6 @@ return [ ...@@ -203,7 +202,6 @@ return [
'URL' => Illuminate\Support\Facades\URL::class, 'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class, 'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class, 'View' => Illuminate\Support\Facades\View::class,
'Batch' => Mavinoo\Batch\BatchFacade::class,
], ],
]; ];
...@@ -1469,11 +1469,6 @@ return array( ...@@ -1469,11 +1469,6 @@ return array(
'Maatwebsite\\Excel\\Readers\\LaravelExcelReader' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php', 'Maatwebsite\\Excel\\Readers\\LaravelExcelReader' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php',
'Maatwebsite\\Excel\\Writers\\CellWriter' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/CellWriter.php', 'Maatwebsite\\Excel\\Writers\\CellWriter' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/CellWriter.php',
'Maatwebsite\\Excel\\Writers\\LaravelExcelWriter' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/LaravelExcelWriter.php', 'Maatwebsite\\Excel\\Writers\\LaravelExcelWriter' => $vendorDir . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/LaravelExcelWriter.php',
'Mavinoo\\Batch\\Batch' => $vendorDir . '/mavinoo/laravel-batch/src/Batch.php',
'Mavinoo\\Batch\\BatchFacade' => $vendorDir . '/mavinoo/laravel-batch/src/BatchFacade.php',
'Mavinoo\\Batch\\BatchInterface' => $vendorDir . '/mavinoo/laravel-batch/src/BatchInterface.php',
'Mavinoo\\Batch\\BatchServiceProvider' => $vendorDir . '/mavinoo/laravel-batch/src/BatchServiceProvider.php',
'Mavinoo\\Batch\\Common\\Common' => $vendorDir . '/mavinoo/laravel-batch/src/Common/Common.php',
'Mockery' => $vendorDir . '/mockery/mockery/library/Mockery.php', 'Mockery' => $vendorDir . '/mockery/mockery/library/Mockery.php',
'Mockery\\Adapter\\Phpunit\\MockeryPHPUnitIntegration' => $vendorDir . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryPHPUnitIntegration.php', 'Mockery\\Adapter\\Phpunit\\MockeryPHPUnitIntegration' => $vendorDir . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryPHPUnitIntegration.php',
'Mockery\\Adapter\\Phpunit\\MockeryTestCase' => $vendorDir . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryTestCase.php', 'Mockery\\Adapter\\Phpunit\\MockeryTestCase' => $vendorDir . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryTestCase.php',
......
...@@ -20,6 +20,5 @@ return array( ...@@ -20,6 +20,5 @@ return array(
'37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
'f0906e6318348a765ffb6eb24e0d0938' => $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php', 'f0906e6318348a765ffb6eb24e0d0938' => $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php',
'58571171fd5812e6e447dce228f52f4d' => $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php', '58571171fd5812e6e447dce228f52f4d' => $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',
'880d9aa7c2c689853e94e8a3794c8282' => $vendorDir . '/mavinoo/laravel-batch/src/Common/Helpers.php',
'cdab8ec77ae0402ce2cb2fdd82a8a401' => $baseDir . '/app/Http/function.php', 'cdab8ec77ae0402ce2cb2fdd82a8a401' => $baseDir . '/app/Http/function.php',
); );
...@@ -36,7 +36,6 @@ return array( ...@@ -36,7 +36,6 @@ return array(
'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'), 'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'),
'PhpAmqpLib\\' => array($vendorDir . '/php-amqplib/php-amqplib/PhpAmqpLib'), 'PhpAmqpLib\\' => array($vendorDir . '/php-amqplib/php-amqplib/PhpAmqpLib'),
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'), 'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'Mavinoo\\Batch\\' => array($vendorDir . '/mavinoo/laravel-batch/src'),
'League\\Flysystem\\' => array($vendorDir . '/league/flysystem/src'), 'League\\Flysystem\\' => array($vendorDir . '/league/flysystem/src'),
'JakubOnderka\\PhpConsoleColor\\' => array($vendorDir . '/jakub-onderka/php-console-color/src'), 'JakubOnderka\\PhpConsoleColor\\' => array($vendorDir . '/jakub-onderka/php-console-color/src'),
'Illuminate\\' => array($vendorDir . '/laravel/framework/src/Illuminate'), 'Illuminate\\' => array($vendorDir . '/laravel/framework/src/Illuminate'),
......
...@@ -21,7 +21,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e ...@@ -21,7 +21,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e
'37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php',
'f0906e6318348a765ffb6eb24e0d0938' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Foundation/helpers.php', 'f0906e6318348a765ffb6eb24e0d0938' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Foundation/helpers.php',
'58571171fd5812e6e447dce228f52f4d' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Support/helpers.php', '58571171fd5812e6e447dce228f52f4d' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Support/helpers.php',
'880d9aa7c2c689853e94e8a3794c8282' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/Common/Helpers.php',
'cdab8ec77ae0402ce2cb2fdd82a8a401' => __DIR__ . '/../..' . '/app/Http/function.php', 'cdab8ec77ae0402ce2cb2fdd82a8a401' => __DIR__ . '/../..' . '/app/Http/function.php',
); );
...@@ -76,7 +75,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e ...@@ -76,7 +75,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e
'M' => 'M' =>
array ( array (
'Monolog\\' => 8, 'Monolog\\' => 8,
'Mavinoo\\Batch\\' => 14,
), ),
'L' => 'L' =>
array ( array (
...@@ -240,10 +238,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e ...@@ -240,10 +238,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e
array ( array (
0 => __DIR__ . '/..' . '/monolog/monolog/src/Monolog', 0 => __DIR__ . '/..' . '/monolog/monolog/src/Monolog',
), ),
'Mavinoo\\Batch\\' =>
array (
0 => __DIR__ . '/..' . '/mavinoo/laravel-batch/src',
),
'League\\Flysystem\\' => 'League\\Flysystem\\' =>
array ( array (
0 => __DIR__ . '/..' . '/league/flysystem/src', 0 => __DIR__ . '/..' . '/league/flysystem/src',
...@@ -1808,11 +1802,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e ...@@ -1808,11 +1802,6 @@ class ComposerStaticInit2a46e5674e69fd121680370ab820bf2e
'Maatwebsite\\Excel\\Readers\\LaravelExcelReader' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php', 'Maatwebsite\\Excel\\Readers\\LaravelExcelReader' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Readers/LaravelExcelReader.php',
'Maatwebsite\\Excel\\Writers\\CellWriter' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/CellWriter.php', 'Maatwebsite\\Excel\\Writers\\CellWriter' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/CellWriter.php',
'Maatwebsite\\Excel\\Writers\\LaravelExcelWriter' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/LaravelExcelWriter.php', 'Maatwebsite\\Excel\\Writers\\LaravelExcelWriter' => __DIR__ . '/..' . '/maatwebsite/excel/src/Maatwebsite/Excel/Writers/LaravelExcelWriter.php',
'Mavinoo\\Batch\\Batch' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/Batch.php',
'Mavinoo\\Batch\\BatchFacade' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/BatchFacade.php',
'Mavinoo\\Batch\\BatchInterface' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/BatchInterface.php',
'Mavinoo\\Batch\\BatchServiceProvider' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/BatchServiceProvider.php',
'Mavinoo\\Batch\\Common\\Common' => __DIR__ . '/..' . '/mavinoo/laravel-batch/src/Common/Common.php',
'Mockery' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery.php', 'Mockery' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery.php',
'Mockery\\Adapter\\Phpunit\\MockeryPHPUnitIntegration' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryPHPUnitIntegration.php', 'Mockery\\Adapter\\Phpunit\\MockeryPHPUnitIntegration' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryPHPUnitIntegration.php',
'Mockery\\Adapter\\Phpunit\\MockeryTestCase' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryTestCase.php', 'Mockery\\Adapter\\Phpunit\\MockeryTestCase' => __DIR__ . '/..' . '/mockery/mockery/library/Mockery/Adapter/Phpunit/MockeryTestCase.php',
......
...@@ -1112,71 +1112,6 @@ ...@@ -1112,71 +1112,6 @@
"install-path": "../maatwebsite/excel" "install-path": "../maatwebsite/excel"
}, },
{ {
"name": "mavinoo/laravel-batch",
"version": "v2.3.4",
"version_normalized": "2.3.4.0",
"source": {
"type": "git",
"url": "https://github.com/mavinoo/laravelBatch.git",
"reference": "126f077dec4ecabb67331694e71bf28b427daa60"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mavinoo/laravelBatch/zipball/126f077dec4ecabb67331694e71bf28b427daa60",
"reference": "126f077dec4ecabb67331694e71bf28b427daa60",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3@dev"
},
"time": "2022-04-20T12:04:23+00:00",
"type": "library",
"extra": {
"laravel": {
"providers": [
"Mavinoo\\Batch\\BatchServiceProvider"
],
"aliases": {
"Batch": "Mavinoo\\Batch\\BatchFacade"
}
}
},
"installation-source": "dist",
"autoload": {
"files": [
"src/Common/Helpers.php"
],
"psr-4": {
"Mavinoo\\Batch\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mohammad Ghanbari",
"email": "mavin.developer@gmail.com"
}
],
"description": "Insert and update batch (bulk) in laravel",
"support": {
"issues": "https://github.com/mavinoo/laravelBatch/issues",
"source": "https://github.com/mavinoo/laravelBatch/tree/v2.3.4"
},
"install-path": "../mavinoo/laravel-batch"
},
{
"name": "mockery/mockery", "name": "mockery/mockery",
"version": "0.9.11", "version": "0.9.11",
"version_normalized": "0.9.11.0", "version_normalized": "0.9.11.0",
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '1db501fcb87906af1b593885d09e450459edb3f8', 'reference' => '6a65bded842127cb11c4764e987e1d101a8b986c',
'name' => 'laravel/laravel', 'name' => 'laravel/laravel',
'dev' => true, 'dev' => true,
), ),
...@@ -328,7 +328,7 @@ ...@@ -328,7 +328,7 @@
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '1db501fcb87906af1b593885d09e450459edb3f8', 'reference' => '6a65bded842127cb11c4764e987e1d101a8b986c',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'league/flysystem' => array( 'league/flysystem' => array(
...@@ -349,15 +349,6 @@ ...@@ -349,15 +349,6 @@
'reference' => 'e0a5afcf86aa4511b291584fae9f00c04e76e60c', 'reference' => 'e0a5afcf86aa4511b291584fae9f00c04e76e60c',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'mavinoo/laravel-batch' => array(
'pretty_version' => 'v2.3.4',
'version' => '2.3.4.0',
'type' => 'library',
'install_path' => __DIR__ . '/../mavinoo/laravel-batch',
'aliases' => array(),
'reference' => '126f077dec4ecabb67331694e71bf28b427daa60',
'dev_requirement' => false,
),
'mockery/mockery' => array( 'mockery/mockery' => array(
'pretty_version' => '0.9.11', 'pretty_version' => '0.9.11',
'version' => '0.9.11.0', 'version' => '0.9.11.0',
......
.idea
vendor
composer.lock
.php_cs.cache
\ No newline at end of file
MIT License
Copyright (c) 2017 Mohammad Ghanbari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Laravel BATCH (BULK)
Insert and update batch (bulk) in laravel
[![License](https://poser.pugx.org/mavinoo/laravel-batch/license)](https://packagist.org/packages/mavinoo/laravel-batch)
[![Latest Stable Version](https://poser.pugx.org/mavinoo/laravel-batch/v/stable)](https://packagist.org/packages/mavinoo/laravel-batch)
[![Total Downloads](https://poser.pugx.org/mavinoo/laravel-batch/downloads)](https://packagist.org/packages/mavinoo/laravel-batch)
[![Daily Downloads](https://poser.pugx.org/mavinoo/laravel-batch/d/daily)](https://packagist.org/packages/mavinoo/laravel-batch)
# 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 1
```php
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);
```
# Example Update 2
```php
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);
```
# Example Increment / Decrement
```php
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);
```
# Example Insert
```php
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);
```
```php
// result : false or array
sample array result:
Array
(
[totalRows] => 384
[totalBatch] => 500
[totalQuery] => 1
)
```
# Helper batch()
```php
// 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
BTC Address: 16XDxkcqiEJ8DYf4xWxu7WK3Peo29TvXxD
{
"name": "mavinoo/laravel-batch",
"description": "Insert and update batch (bulk) in laravel",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Mohammad Ghanbari",
"email": "mavin.developer@gmail.com"
}
],
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3@dev"
},
"autoload": {
"psr-4": {
"Mavinoo\\Batch\\": "src/"
},
"files": [
"src/Common/Helpers.php"
]
},
"extra": {
"laravel": {
"providers": [
"Mavinoo\\Batch\\BatchServiceProvider"
],
"aliases": {
"Batch": "Mavinoo\\Batch\\BatchFacade"
}
}
},
"minimum-stability": "dev"
}
<?php declare(strict_types=1);
namespace Mavinoo\Batch;
use Illuminate\Support\Facades\Facade;
class BatchFacade extends Facade
{
/**
* Get facade accessor to retrieve instance.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Batch';
}
}
<?php declare(strict_types=1);
namespace Mavinoo\Batch;
use Illuminate\Database\Eloquent\Model;
interface BatchInterface
{
/**
* Update multiple rows.
*
* @param Model $table
* @param array $values
* @param string|null $index
* @param bool $raw
* @return mixed
*/
public function update(Model $table, array $values, string $index = null, bool $raw = false);
/**
* Update multiple rows with two index.
*
* @param Model $table
* @param array $values
* @param string|null $index
* @param string|null $index2
* @param bool $raw
* @return mixed
*/
public function updateWithTwoIndex(Model $table, array $values, string $index = null, string $index2 = null, bool $raw = false);
/**
* Insert multiple rows.
*
* @param Model $table
* @param array $columns
* @param array $values
* @param int $batchSize
* @param bool $insertIgnore
* @return mixed
*/
public function insert(Model $table, array $columns, array $values, int $batchSize = 500, bool $insertIgnore = false);
}
<?php declare(strict_types=1);
namespace Mavinoo\Batch;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\DatabaseManager;
class BatchServiceProvider extends ServiceProvider
{
/**
* Register Batch instance to IOC.
*
* @updateedBy Ibrahim Sakr <ebrahimes@gmail.com>
*/
public function register()
{
$this->app->bind('Batch', function ($app) {
return new Batch($app->make(DatabaseManager::class));
});
}
}
<?php declare(strict_types=1);
namespace Mavinoo\Batch\Common;
class Common
{
/**
* Escape values according to mysql.
*
* @param $fieldValue
* @return array|string|string[]
*/
public static function mysql_escape($fieldValue)
{
if (is_array($fieldValue)) {
return array_map(__METHOD__, $fieldValue);
}
if (is_bool($fieldValue)) {
return (int)$fieldValue;
}
if (self::is_json($fieldValue)) {
return self::safeJson($fieldValue);
}
if (!empty($fieldValue) && is_string($fieldValue)) {
return str_replace(
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
$fieldValue
);
}
return $fieldValue;
}
/**
* Disable Backtick.
*
* @param $drive
* @return boolean
*/
public static function disableBacktick($drive)
{
return in_array($drive, ['pgsql', 'sqlsrv']);
}
protected static function safeJsonString($fieldValue)
{
return str_replace(
["'"],
["''"],
$fieldValue
);
}
protected static function is_json($str): bool
{
if (!is_string($str) || is_numeric($str)) {
return false;
}
$json = json_decode($str);
return $json && $str != $json;
}
protected static function safeJson($jsonData, $asArray = false)
{
$jsonData = json_decode($jsonData, true);
$safeJsonData = [];
if (!is_array($jsonData)) {
return $jsonData;
}
foreach ($jsonData as $key => $value) {
if (self::is_json($value)) {
$safeJsonData[$key] = self::safeJson($value, true);
} elseif (is_string($value)) {
$safeJsonData[$key] = self::safeJsonString($value);
} elseif (is_array($value)) {
$safeJsonData[$key] = self::safeJson(json_encode($value), true);
} else {
$safeJsonData[$key] = $value;
}
}
return $asArray ? $safeJsonData : json_encode($safeJsonData, JSON_UNESCAPED_UNICODE);
}
}
<?php declare(strict_types=1);
if (!function_exists('batch')) {
/**
* Batch helper to get Mavino\Batch\Batch instance.
*
* @return mixed
*/
function batch()
{
return app('Mavinoo\Batch\Batch');
}
}
<?php declare(strict_types=1);
use Mavinoo\Batch\Batch;
require_once 'BootstrapDatabase.php';
class BatchInsertTest extends BootstrapDatabase
{
public $columns = [
'email',
'password',
'name',
'status',
'is_vip'
];
public function testBatchInsertWithFacade()
{
$values = [
[
'djunehor@gmail.com',
bcrypt('djunehor'),
'djunehor',
'active',
true,
],
[
'samuel@gmail.com',
bcrypt('samuel'),
'samuel',
'whodey',
false,
],
[
'general@gmail.com',
bcrypt('general'),
'general',
'inactive',
false,
]
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
$this->assertIsArray($result);
$this->assertTrue($result['totalRows'] === 3);
$this->assertTrue($result['totalBatch'] === 500);
$this->model->truncate();
}
public function testBatchInsertIncorrectColumnCount()
{
$columns = [
'email',
'password',
'name',
'status',
];
$values = [
[
'djunehor@gmail.com',
bcrypt('djunehor'),
'djunehor',
'active',
],
[
'samuel@gmail.com',
bcrypt('samuel'),
'samuel',
'whodey',
],
[
'general@gmail.com',
bcrypt('general'),
'general',
'inactive',
]
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
$this->assertFalse($result);
}
public function testBatchInsertWithHelper()
{
$values = [
[
'djunehor@gmail.com',
bcrypt('djunehor'),
'djunehor',
'active',
true,
],
[
'samuel@gmail.com',
bcrypt('samuel'),
'samuel',
'whodey',
false,
],
[
'general@gmail.com',
bcrypt('general'),
'general',
'inactive',
false,
]
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
$result = batch()->insert($this->model, $this->columns, $values, $batchSize);
$this->assertIsArray($result);
$this->assertTrue($result['totalRows'] === 3);
$this->assertTrue($result['totalBatch'] === 500);
$this->model->truncate();
}
}
<?php declare(strict_types=1);
require_once 'BootstrapDatabase.php';
use Carbon\Carbon;
use Mavinoo\Batch\Batch;
class BatchUpdateTest extends BootstrapDatabase
{
public $columns = [
'email',
'password',
'name',
'status',
'is_vip',
];
private function insert()
{
$values = [
[
'djunehor@gmail.com',
bcrypt('djunehor'),
'djunehor',
'active',
true,
],
[
'samuel@gmail.com',
bcrypt('samuel'),
'samuel',
'whodey',
false,
],
[
'general@gmail.com',
bcrypt('general'),
'general',
'inactive',
false,
]
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
$result = Batch::insert($this->model, $this->columns, $values, $batchSize);
$this->assertIsArray($result);
$this->assertTrue($result['totalRows'] === 3);
$this->assertTrue($result['totalBatch'] === 500);
}
public function testBatchUpdateWithFacade()
{
$this->insert();
$columnValues = [
[
'id' => 1,
'name' => 'amala',
'is_vip' => false
],
[
'id' => 2,
'status' => 'deactive',
'name' => 'Ghanbari'
],
[
'id' => 3,
'status' => 'active',
'created_at' => Carbon::now()
]
];
$index = 'id';
$result = Batch::update($this->model, $columnValues, $index);
$member = $this->model->findOrFail(1);
$this->assertTrue($result === 3);
$this->assertEquals('amala', $member->name);
$this->assertFalse($member->is_vip);
$this->model->truncate();
}
public function testBatchUpdateWithHelper()
{
$this->insert();
$columnValues = [
[
'id' => 1,
'name' => 'amala',
'is_vip' => false
],
[
'id' => 2,
'status' => 'deactive',
'name' => 'Ghanbari'
],
[
'id' => 3,
'status' => 'active',
'created_at' => Carbon::now()
]
];
$index = 'id';
$result = batch()->update($this->model, $columnValues, $index);
$member = $this->model->findOrFail(1);
$this->assertTrue($result === 3);
$this->assertEquals('amala', $member->name);
$this->assertFalse($member->is_vip);
$this->model->truncate();
}
}
<?php declare(strict_types=1);
require_once 'TestModel.php';
use Tests\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
class BootstrapDatabase extends TestCase
{
public $model;
public function setUp(): void
{
parent::setUp();
$tableName = (new TestModel())->tableName();
if (! Schema::hasTable($tableName)) {
Schema::create($tableName, function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('status')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
$this->model = new TestModel();
$this->model->truncate();
}
public function tearDown(): void
{
Schema::dropIfExists((new TestModel())->tableName());
}
}
<?php declare(strict_types=1);
class TestModel extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'HmKTjCJgLN9bBq7KXzI3';
protected $casts = [
'is_vip' => 'boolean'
];
/**
* Get name of the table.
*
* @return string
*/
public function tableName()
{
return $this->table;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment