-
-
Notifications
You must be signed in to change notification settings - Fork 0
Update Item
Edits an existing item's attributes, or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values. You can also perform a conditional update on an existing item (insert a new attribute name-value pair if it doesn't exist, or replace an existing name-value pair if it has certain expected attribute values).
An expression that defines one or more attributes to be updated, the action to be performed on them, and new values for them.
DB::table('Users')->key($someKey)->update([
'name' => 'Lorem'
]);
Also you can use DynamoDB update action:
DB::table('Users')->key($someKey)->update([
'first_name' => 'Lorem', // SET
'name' => null, // REMOVE
'add:count' => 1, // ADD
'delete:lists' => 'one' // DELETE
]);
A condition that must be satisfied in order for a conditional update to succeed.
An expression can contain any of the following methods:
condition($column, $operator, $value = null)
conditionAttributeExists($path)
orConditionAttributeExists($path)
conditionAttributeNotExists($path)
orConditionAttributeNotExists($path)
conditionAttributeType($path, $type)
orConditionAttributeType($path, $type)
conditionBeginsWith($path, $substr)
orConditionBeginsWith($path, $substr)
conditionContains($path, $operand)
orConditionContains($path, $operand)
conditionSize($column, $operator, $value = null, string $type = 'and')
DB::table('Users')->key($someKey)->conditionBeginsWith('name', 'BD_')->update([
'add' => 'Lorem'
]);
For more information you can see DynamoDB Doc
Use returnValues
method if you want to get the item attributes as they appear before or after they are updated. For Update
, the valid values are:
ReturnValues::NONE
- If ReturnValues is not specified, or if its value is NONE, then nothing is returned. (This setting is the default for ReturnValues.)
ReturnValues::ALL_OLD
- Returns all of the attributes of the item, as they appeared before the UpdateItem operation.
ReturnValues::UPDATED_OLD
- Returns only the updated attributes, as they appeared before the UpdateItem operation.
ReturnValues::ALL_NEW
- Returns all of the attributes of the item, as they appear after the UpdateItem operation.
ReturnValues::UPDATED_NEW
- Returns only the updated attributes, as they appear after the UpdateItem operation.
DB::table('Users')->key($someKey)->returnValues(ReturnValues::UPDATED_NEW)->update([
'add' => 'Lorem'
]);
// Update method will return all new updated attributes.
The query builder also provides convenient methods for incrementing or decrementing the value of a given column. Both of these methods accept at least one argument: the column to modify. A second argument may be provided to specify the amount by which the column should be incremented or decremented:
DB::table('users')->key(['id' => 'id-1'])->increment('votes');
DB::table('users')->key(['id' => 'id-1'])->increment('votes', 5);
DB::table('users')->key(['id' => 'id-1'])->decrement('votes');
DB::table('users')->key(['id' => 'id-1'])->decrement('votes', 5);
Also you can pass additional parameters for update:
DB::table('users')->key(['id' => 'id-1'])->increment('votes', 1, [
'updated_at' => (string) now()
]);
You may also specify additional columns to update during the operation:
DB::table('users')->key(['id' => 'id-1'])->increment('votes', 1, ['name' => 'John']);
You may also specify add
or set
mode, by default increment or decrement, is using set
mode. But you can add the prefix add:
to use add
mode.
DB::table('users')->key(['id' => 'id-1'])->decrement('add:votes', 5);