Skip to content

Commit 8e958b5

Browse files
Update Builder.php
add incrementEach add decrementEach
1 parent 1303b5f commit 8e958b5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Query/Builder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,29 @@ public function increment($column, $amount = 1, array $extra = [], array $option
647647
return $this->performUpdate($query, $options);
648648
}
649649

650+
public function incrementEach(array $columns, array $extra = [], array $options = [])
651+
{
652+
653+
$query = ['$inc' => $columns];
654+
655+
if (! empty($extra)) {
656+
$query['$set'] = $extra;
657+
}
658+
659+
// Protect
660+
foreach ($columns as $column => $amount) {
661+
$this->where(function ($query) use ($column) {
662+
$query->where($column, 'exists', false);
663+
664+
$query->orWhereNotNull($column);
665+
});
666+
}
667+
668+
$options = $this->inheritConnectionOptions($options);
669+
670+
return $this->performUpdate($query, $options);
671+
}
672+
650673
/**
651674
* @inheritdoc
652675
*/
@@ -655,6 +678,17 @@ public function decrement($column, $amount = 1, array $extra = [], array $option
655678
return $this->increment($column, -1 * $amount, $extra, $options);
656679
}
657680

681+
public function decrementEach(array $columns, array $extra = [], array $options = [])
682+
{
683+
$decrement = [];
684+
685+
foreach ($columns as $column => $amount) {
686+
$decrement[$column] = -1 * $amount;
687+
}
688+
689+
return $this->incrementEach($decrement, $extra, $options);
690+
}
691+
658692
/**
659693
* @inheritdoc
660694
*/

0 commit comments

Comments
 (0)