update many laravel 1

update many laravel

$post->comments()->updateMany([
    [
        'message' => 'A new comment.',
    ],
    [
        'message' => 'Another new comment.',
    ],
]);

Here is what the above code is Doing:
1. We’re retrieving the post with the ID of 1.
2. We’re retrieving the comments relationship.
3. We’re updating the comments with the IDs of 1 and 2.
4. We’re updating the message column of the comments.

The updateMany method accepts an array of arrays. Each array represents a single record that should be updated.

The updateMany method will return the number of records that were updated.

You can also use the updateExistingPivot method to update a single record:

$post->comments()->updateExistingPivot(1, [
‘message’ => ‘A new comment.’,
]);

Similar Posts