Skip to content

Commit 133e278

Browse files
committed
add method update Multiple Condition.
1 parent d931f44 commit 133e278

File tree

3 files changed

+194
-13
lines changed

3 files changed

+194
-13
lines changed

README.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
11
# Laravel BATCH (BULK)
2+
23
Insert and update batch (bulk) in laravel
34

45
[![License](https://poser.pugx.org/mavinoo/laravel-batch/license)](https://packagist.org/packages/mavinoo/laravel-batch)
56
[![Latest Stable Version](https://poser.pugx.org/mavinoo/laravel-batch/v/stable)](https://packagist.org/packages/mavinoo/laravel-batch)
67
[![Total Downloads](https://poser.pugx.org/mavinoo/laravel-batch/downloads)](https://packagist.org/packages/mavinoo/laravel-batch)
78
[![Daily Downloads](https://poser.pugx.org/mavinoo/laravel-batch/d/daily)](https://packagist.org/packages/mavinoo/laravel-batch)
89

9-
1010
# Install
11+
1112
`composer require mavinoo/laravel-batch`
1213

1314
# Service Provider
15+
1416
file app.php in array providers :
1517

1618
`Mavinoo\Batch\BatchServiceProvider::class,`
1719

1820
# Aliases
21+
1922
file app.php in array aliases :
2023

2124
`'Batch' => Mavinoo\Batch\BatchFacade::class,`
2225

23-
# Example Update 1
26+
# Example Update Multiple Condition
27+
28+
```php
29+
use App\Models\User;
30+
31+
$userInstance = new User;
32+
$arrays = [
33+
[
34+
'conditions' => ['id' => 1, 'status' => 'active'],
35+
'columns' => [
36+
'status' => 'invalid'
37+
'nickname' => 'mohammad'
38+
],
39+
],
40+
[
41+
'conditions' => ['id' => 2],
42+
'columns' => [
43+
'nickname' => 'mavinoo',
44+
'name' => 'mohammad',
45+
],
46+
],
47+
[
48+
'conditions' => ['id' => 3],
49+
'columns' => [
50+
'nickname' => 'ali'
51+
],
52+
],
53+
];
54+
$keyName = 'id';
55+
56+
Batch::updateMultipleCondition($userInstance, $arrays, $keyName);
57+
or
58+
batch()->updateMultipleCondition($userInstance, $arrays, $keyName);
59+
```
60+
61+
# Example Update 2
2462

2563
```php
2664
use App\Models\User;
@@ -41,9 +79,11 @@ $value = [
4179
$index = 'id';
4280

4381
Batch::update($userInstance, $value, $index);
82+
or
83+
batch()->update($userInstance, $values, $index);
4484
```
4585

46-
# Example Update 2
86+
# Example Update 3
4787

4888
```php
4989
use App\Models\User;
@@ -72,6 +112,8 @@ $value = [
72112
$index = 'id';
73113

74114
Batch::update($userInstance, $value, $index);
115+
or
116+
batch()->update($userInstance, $values, $index);
75117
```
76118

77119
# Example Increment / Decrement
@@ -105,6 +147,8 @@ $value = [
105147
$index = 'id';
106148

107149
Batch::update($userInstance, $value, $index);
150+
or
151+
batch()->update($userInstance, $values, $index);
108152
```
109153

110154
# Example Insert
@@ -146,9 +190,10 @@ $values = [
146190
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
147191

148192
$result = Batch::insert($userInstance, $columns, $values, $batchSize);
193+
or
194+
$result = batch()->insert($userInstance, $values, $index);
149195
```
150196

151-
152197
```php
153198
// result : false or array
154199

@@ -202,9 +247,11 @@ $result = batch()->insert($userInstance, $columns, $values, $batchSize);
202247
```
203248

204249
# Tests
250+
205251
If you don't have phpunit installed on your project, first run `composer require phpunit/phpunit`
206252

207253
In the root of your laravel app, run `./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests`
208254

209-
# Donate
255+
# Donate
256+
210257
USDT Address: 0x98410956169cdd00a43fe895303bdca096f37062

src/Batch.php

Lines changed: 131 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(DatabaseManager $db)
5050
* @param string $index
5151
* @param bool $raw
5252
* @return bool|int
53+
* @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
5354
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
5455
*/
5556
public function update(Model $table, array $values, string $index = null, bool $raw = false)
@@ -119,7 +120,7 @@ public function update(Model $table, array $values, string $index = null, bool $
119120
$cases = '';
120121
foreach ($final as $k => $v) {
121122
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
122-
. 'ELSE "' . $k . '" END), ';
123+
. 'ELSE "' . $k . '" END), ';
123124
}
124125

125126
$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "');";
@@ -129,14 +130,13 @@ public function update(Model $table, array $values, string $index = null, bool $
129130
$cases = '';
130131
foreach ($final as $k => $v) {
131132
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
132-
. 'ELSE `' . $k . '` END), ';
133+
. 'ELSE `' . $k . '` END), ';
133134
}
134135

135136
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";
136137

137138
}
138139

139-
140140
return $this->db->connection($this->getConnectionName($table))->update($query);
141141
}
142142

@@ -148,6 +148,7 @@ public function update(Model $table, array $values, string $index = null, bool $
148148
* @param string|null $index2
149149
* @param bool $raw
150150
* @return bool|int
151+
* @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
151152
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
152153
*
153154
* @desc
@@ -205,7 +206,7 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
205206
$cases = '';
206207
foreach ($final as $k => $v) {
207208
$cases .= '"' . $k . '" = (CASE ' . implode("\n", $v) . "\n"
208-
. 'ELSE "' . $k . '" END), ';
209+
. 'ELSE "' . $k . '" END), ';
209210
}
210211

211212
$query = "UPDATE \"" . $this->getFullTableName($table) . '" SET ' . substr($cases, 0, -2) . " WHERE \"$index\" IN('" . implode("','", $ids) . "') AND \"$index2\" IN('" . implode("','", $ids2) . "');";
@@ -214,14 +215,135 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
214215
$cases = '';
215216
foreach ($final as $k => $v) {
216217
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
217-
. 'ELSE `' . $k . '` END), ';
218+
. 'ELSE `' . $k . '` END), ';
218219
}
219220
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' . " );";
220221
}
221222

222223
return $this->db->connection($this->getConnectionName($table))->update($query);
223224
}
224225

226+
/**
227+
* Update multiple condition rows
228+
* @param Model $table
229+
* @param array $arrays
230+
* @param string $keyName
231+
* @param bool $raw
232+
* @return bool|int
233+
* @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
234+
*
235+
* @desc
236+
* Example
237+
* $table = new \App\Models\User;
238+
* $arrays = [
239+
* [
240+
* 'conditions' => ['id' => 1, 'status' => 'active'],
241+
* 'columns' => [
242+
* 'status' => 'invalid'
243+
* 'nickname' => 'mohammad'
244+
* ],
245+
* ],
246+
* [
247+
* 'conditions' => ['id' => 2],
248+
* 'columns' => [
249+
* 'nickname' => 'mavinoo',
250+
* 'name' => 'mohammad',
251+
* ],
252+
* ],
253+
* [
254+
* 'conditions' => ['id' => 3],
255+
* 'columns' => [
256+
* 'nickname' => 'ali'
257+
* ],
258+
* ],
259+
* ];
260+
* $keyName = 'id';
261+
*
262+
*/
263+
public function updateMultipleCondition(Model $table, array $arrays, string $keyName = null, bool $raw = false)
264+
{
265+
$driver = $table->getConnection()->getDriverName();
266+
$connectionName = $this->getConnectionName($table);
267+
$tableName = $this->getFullTableName($table);
268+
$timestamp = $table->usesTimestamps();
269+
$backtick = Common::disableBacktick($driver) ? '`' : '';
270+
271+
if (!count($arrays)) {
272+
return false;
273+
}
274+
275+
if (!isset($keyName) || empty($keyName)) {
276+
$keyName = $table->getKeyName();
277+
}
278+
279+
$columns = [];
280+
$conditionMaster = [];
281+
foreach ($arrays as $array) {
282+
foreach ($array['conditions'] as $keyCondition => $condition) {
283+
if ($keyName == $keyCondition and !in_array($condition, $conditionMaster)) {
284+
$conditionMaster[] = str(Common::mysql_escape($condition))->toString();
285+
}
286+
}
287+
foreach ($array as $key => $item) {
288+
if ($key == 'columns') {
289+
foreach ($item as $k => $value) {
290+
if (!in_array($key, $columns)) {
291+
$columns[$k] = $k;
292+
}
293+
}
294+
}
295+
}
296+
}
297+
298+
$arraysNew = [];
299+
$keys = array_keys($columns);
300+
foreach ($keys as $key) {
301+
$arraysMixed = collect($arrays)->filter(function ($rows) use ($key) {
302+
return in_array($key, array_keys($rows['columns']));
303+
});
304+
305+
foreach ($arraysMixed as $item) {
306+
$value = $raw ? Common::mysql_escape($item['columns'][$key]) : "'" . Common::mysql_escape($item['columns'][$key]) . "'";
307+
$arraysNew[$key][] = [
308+
'conditions' => $item['conditions'],
309+
'value' => is_null($item['columns'][$key]) ? "NULL" : $value,
310+
];
311+
312+
if ($timestamp) {
313+
$arraysNew['updated_at'][] = [
314+
'conditions' => $item['conditions'],
315+
'value' => "'".(now())."'",
316+
];
317+
}
318+
}
319+
}
320+
321+
$cases = [];
322+
foreach ($arraysNew as $key => $items) {
323+
$caseSql = "{$backtick}{$key}{$backtick} = (CASE ";
324+
foreach ($items as $item) {
325+
$conditions = $item['conditions'];
326+
$value = $item['value'];
327+
328+
$conditionContext = [];
329+
foreach ($conditions as $conditionKey => $condition) {
330+
$conditionContext[] = " {$backtick}{$conditionKey}{$backtick} = '{$condition}' ";
331+
}
332+
333+
$conditionContext = join(' and ', $conditionContext);
334+
$caseSql .= " WHEN $conditionContext THEN {$value} ";
335+
}
336+
$caseSql .= " ELSE {$backtick}{$key}{$backtick} END)";
337+
$cases[] = $caseSql;
338+
}
339+
$caseSql = join(', ', $cases);
340+
$conditionMaster = join(', ', $conditionMaster);
341+
342+
$query = "update {$backtick}{$tableName}{$backtick} set {$caseSql} where {$backtick}{$keyName}{$backtick} in ({$conditionMaster})";
343+
344+
return $this->db->connection($connectionName)->update($query);
345+
}
346+
225347
/**
226348
* Insert Multi rows.
227349
*
@@ -232,6 +354,7 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
232354
* @param bool $insertIgnore
233355
* @return bool|mixed
234356
* @throws \Throwable
357+
* @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
235358
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
236359
* @desc
237360
* Example
@@ -360,9 +483,9 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
360483
}
361484

362485
return [
363-
'totalRows' => $totalValues,
364-
'totalBatch' => $totalChunk,
365-
'totalQuery' => $totalQuery
486+
'totalRows' => $totalValues,
487+
'totalBatch' => $totalChunk,
488+
'totalQuery' => $totalQuery
366489
];
367490
});
368491
}

src/BatchInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ public function update(Model $table, array $values, string $index = null, bool $
2929
*/
3030
public function updateWithTwoIndex(Model $table, array $values, string $index = null, string $index2 = null, bool $raw = false);
3131

32+
/**
33+
* Update multiple condition rows.
34+
*
35+
* @param Model $table
36+
* @param array $values
37+
* @param string|null $index
38+
* @param bool $raw
39+
* @return mixed
40+
*/
41+
public function updateMultipleCondition(Model $table, array $values, string $index = null, bool $raw = false);
42+
3243
/**
3344
* Insert multiple rows.
3445
*

0 commit comments

Comments
 (0)