Skip to content

Commit 9ec7dd1

Browse files
[8.x] Add updateOrFail method to Model (#38592)
* Add `updateOrFail` method to `Model` This makes it more convenient to handle update errors in update actions. * Update Model.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent d2de6f8 commit 9ec7dd1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,24 @@ public function update(array $attributes = [], array $options = [])
877877
return $this->fill($attributes)->save($options);
878878
}
879879

880+
/**
881+
* Update the model in the database within a transaction.
882+
*
883+
* @param array $attributes
884+
* @param array $options
885+
* @return bool
886+
*
887+
* @throws \Throwable
888+
*/
889+
public function updateOrFail(array $attributes = [], array $options = [])
890+
{
891+
if (! $this->exists) {
892+
return false;
893+
}
894+
895+
return $this->fill($attributes)->saveOrFail($options);
896+
}
897+
880898
/**
881899
* Update the model in the database without raising any events.
882900
*

0 commit comments

Comments
 (0)