Skip to content

Conversation

@caugner
Copy link
Contributor

@caugner caugner commented Aug 30, 2021

The updateOrFail() method makes it more convenient to handle update errors in update actions, as it is less verbose than checking the return value of update().

@crissi
Copy link
Contributor

crissi commented Aug 30, 2021

sql drivers always throw an exception? what driver are you using?

@caugner
Copy link
Contributor Author

caugner commented Aug 30, 2021

@crissi I'm using the MySQL driver, but what is the difference between save() and saveOrFail() if both throw an Exception?

@crissi
Copy link
Contributor

crissi commented Aug 30, 2021

I see... did not know saveOrFail or existed.

This makes it more convenient to handle update errors in update actions.
@caugner
Copy link
Contributor Author

caugner commented Aug 30, 2021

The PHPDoc confirms my assumption that saveOrFail() throws, while update() doesn't:

/**
* Update the model in the database.
*
* @param array $attributes
* @param array $options
* @return bool
*/
public function update(array $attributes = [], array $options = [])

/**
* Save the model to the database using transaction.
*
* @param array $options
* @return bool
*
* @throws \Throwable
*/
public function saveOrFail(array $options = [])

@caugner
Copy link
Contributor Author

caugner commented Aug 30, 2021

Note: saveOrFail() was added in #11202.

@taylorotwell taylorotwell merged commit 9ec7dd1 into laravel:8.x Aug 30, 2021
@caugner caugner deleted the patch-2 branch August 30, 2021 15:05
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants