Skip to content

Commit 7eb48e6

Browse files
authored
Merge pull request mavinoo#56 from WalterWoshid/master
Added increment / decrement functionality
2 parents 1fbd6cb + dd84db6 commit 7eb48e6

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ $index = 'id';
7474
Batch::update($userInstance, $value, $index);
7575
```
7676

77+
# Example Increment / Decrement
78+
79+
```php
80+
use App\Models\User;
81+
82+
$userInstance = new User;
83+
$value = [
84+
[
85+
'id' => 1,
86+
'balance' => '+500' // Add
87+
] ,
88+
[
89+
'id' => 2,
90+
'balance' => '-200' // Subtract
91+
] ,
92+
[
93+
'id' => 3,
94+
'balance' => '*5' // Multiply
95+
] ,
96+
[
97+
'id' => 4,
98+
'balance' => '/2' // Divide
99+
] ,
100+
[
101+
'id' => 5,
102+
'balance' => '%2' // Modulo
103+
] ,
104+
];
105+
$index = 'id';
106+
107+
Batch::update($userInstance, $value, $index);
108+
```
109+
77110
# Example Insert
78111

79112
```php

src/Batch.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ public function update(Model $table, array $values, string $index = null, bool $
7373

7474
foreach (array_keys($val) as $field) {
7575
if ($field !== $index) {
76-
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
77-
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
76+
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) {
77+
$value = '`' . $field . '`' . $val[$field];
78+
} else {
79+
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
80+
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
81+
}
7882
if ($driver == 'pgsql')
7983
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
8084
else

0 commit comments

Comments
 (0)