Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ $userInstance = new User;
$value = [
[
'id' => 1,
'balance' => '+500' // Add
'balance' => ['+', 500] // Add
] ,
[
'id' => 2,
'balance' => '-200' // Subtract
'balance' => ['-', 200] // Subtract
] ,
[
'id' => 3,
'balance' => '*5' // Multiply
'balance' => ['*', 5] // Multiply
] ,
[
'id' => 4,
'balance' => '/2' // Divide
'balance' => ['/', 2] // Divide
] ,
[
'id' => 5,
'balance' => '%2' // Modulo
'balance' => ['%', 2] // Modulo
] ,
];
$index = 'id';
Expand Down
51 changes: 37 additions & 14 deletions src/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,37 @@ public function __construct(DatabaseManager $db)
}

/**
* Update multiple rows.
* <h2>Update multiple rows.</h2>
*
* @param Model $table
* @param array $values
* @param string $index
* @param bool $raw
* @return bool|int
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
* @desc
* Example
* $table = 'users';
* Example:<br>
* ```
* $userInstance = new \App\Models\User;
* $value = [
* [
* 'id' => 1,
* 'status' => 'active',
* 'nickname' => 'Mohammad'
* ] ,
* ],
* [
* 'id' => 5,
* 'status' => 'deactive',
* 'nickname' => 'Ghanbari'
* ] ,
* ],
* [
* 'id' => 7,
* 'balance' => ['+', 500]
* ]
* ];
* $index = 'id';
* Batch::update($userInstance, $value, $index);
* ```
*
* @param \Illuminate\Database\Eloquent\Model $table
* @param array $values
* @param string $index
* @param bool $raw
* @return bool|int
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
*/
public function update(Model $table, array $values, string $index = null, bool $raw = false)
{
Expand Down Expand Up @@ -73,12 +80,28 @@ public function update(Model $table, array $values, string $index = null, bool $

foreach (array_keys($val) as $field) {
if ($field !== $index) {
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) {
$value = '`' . $field . '`' . $val[$field];
// If increment / decrement
if (gettype($val[$field]) == 'array') {
// If array has two values
if (!array_key_exists(0, $val[$field]) || !array_key_exists(1, $val[$field])) {
throw new \ArgumentCountError('Increment/Decrement array needs to have 2 values, a math operator (+, -, *, /, %) and a number');
}
// Check first value
if (gettype($val[$field][0]) != 'string' || !in_array($val[$field][0], ['+', '-', '*', '/', '%'])) {
throw new \TypeError('First value in Increment/Decrement array needs to be a string and a math operator (+, -, *, /, %)');
}
// Check second value
if (!is_numeric($val[$field][1])) {
throw new \TypeError('Second value in Increment/Decrement array needs to be numeric');
}
// Increment / decrement
$value = '`' . $field . '`' . $val[$field][0] . $val[$field][1];
} else {
// Only update
$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