Skip to content

Commit

Permalink
[11.x] Allow callback to be passed to updateOrInsert() to pass diff…
Browse files Browse the repository at this point in the history
…erent `$values` if the record already exists (#51566)

* allow a callback to be passed to `updateOrInsert()` to pass different `$values` if the record already exists

* follow Laravel codestyle

* update method signature to add parameter type hinting
  • Loading branch information
Markshall authored May 30, 2024
1 parent f993943 commit 92eda54
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3749,12 +3749,18 @@ public function updateFrom(array $values)
* Insert or update a record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @param array|callable $values
* @return bool
*/
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));
}

Expand Down

0 comments on commit 92eda54

Please sign in to comment.