Skip to content

Commit

Permalink
Merge pull request mavinoo#56 from WalterWoshid/master
Browse files Browse the repository at this point in the history
Added increment / decrement functionality
  • Loading branch information
mavinoo authored Feb 23, 2021
2 parents 1fbd6cb + dd84db6 commit 7eb48e6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ $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
Expand Down
8 changes: 6 additions & 2 deletions src/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ public function update(Model $table, array $values, string $index = null, bool $

foreach (array_keys($val) as $field) {
if ($field !== $index) {
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) {
$value = '`' . $field . '`' . $val[$field];
} else {
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
}
if ($driver == 'pgsql')
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
else
Expand Down

0 comments on commit 7eb48e6

Please sign in to comment.