Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add updateOrFail method to Model #38592

Merged
merged 2 commits into from
Aug 30, 2021
Merged

[8.x] Add updateOrFail method to Model #38592

merged 2 commits into from
Aug 30, 2021

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 <taylor@laravel.com>
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.

None yet

3 participants