Skip to content

Commit

Permalink
[11.x] Add example for updateOrInsert method in queries (#9695)
Browse files Browse the repository at this point in the history
* Update queries.md

* Update queries.md

* Update queries.md

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
milwad-dev and taylorotwell authored Jun 9, 2024
1 parent 45770c3 commit ef9f016
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,22 @@ The `updateOrInsert` method will attempt to locate a matching database record us
['votes' => '2']
);

You may provide a closure to the `updateOrInsert` method to customize the attributes that are updated or inserted into the database based on the existence of a matching record:

```php
DB::table('users')->updateOrInsert(
['user_id' => $user_id],
fn ($exists) => $exists ? [
'name' => $data['name'],
'email' => $data['email'],
] : [
'name' => $data['name'],
'email' => $data['email'],
'marketable' => true,
],
);
```

<a name="updating-json-columns"></a>
### Updating JSON Columns

Expand Down

0 comments on commit ef9f016

Please sign in to comment.