Skip to content

Commit

Permalink
fix: updateOrInsert signature change in 11.10 (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama authored Jun 10, 2024
1 parent fbed84c commit 60d8e79
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Colopl\Spanner\Query;

use Closure;
use Colopl\Spanner\Connection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Query\Builder as BaseBuilder;
Expand Down Expand Up @@ -61,13 +62,23 @@ public function update(array $values)
/**
* @inheritDoc
*/
public function updateOrInsert(array $attributes, array $values = [])
public function updateOrInsert(array $attributes, array|callable $values = [])
{
if (! $this->where($attributes)->exists()) {
$exists = $this->where($attributes)->exists();

if ($values instanceof Closure) {
$values = $values($exists);
}

if (! $exists) {
return $this->insert(array_merge($attributes, $values));
}

return (bool) $this->take(1)->update(Arr::except($values, array_keys($attributes)));
if (empty($values)) {
return true;
}

return (bool) $this->limit(1)->update(Arr::except($values, array_keys($attributes)));
}

/**
Expand Down

0 comments on commit 60d8e79

Please sign in to comment.