

Differently from C++ where we can use multiple inheritance, in PHP a Trait is the mechanism to reduce limitations around single inheritance.īesides that, I have a personal rule where I use Traits only when an implementation gets highly reused, since most of my controllers end up having an update action, in my context, this is something highly reused. Since this abstraction interacts with models, I wanted to avoid using inheritance because it would be a coupling too high for an abstraction as simple as this one.įurthermore, I want to leave the inheritance in the models open for usage, whether by a team member decision or by some specific use case.įor that reason, I’ve chosen to implement the abstraction in a Trait. Let’s see how we can abstract this implementation in a way that we get high reusability with simple usage out of this abstraction.

If we had a standard *orFail() function, this function would be the simplest way of implementing an update action in a controller, completely removing the need for the approach that will be explained in the next section.
LARAVEL ELOQUENT UPDATE RETURN SERIES
If that was the case, we could just automate the handling of the exception thrown by the updateOrFail() function the same way we did in the last post from my series about exceptions. This is a very odd implementation in the framework, since most of the *orFail() functions usually throws an exception. NOTE: Notice that the updateOrFail() function doesn't have any significant difference from the update() function, and the resultant usage is exactly the same as the one presented in the previous section. In case it returns false, an error message with status 400 will be responded.Īnd again, if the user got correctly updated, we render the updated user as a response. Next, we have a second conditional where we are calling $user->updateOrFail() with the data that we want to update, this function returns true or false to let us know if the data got successfully updated.

Now that we’ve seen two different ways of implementing the update action on Laravel, let’s see how we can implement this using the updateOrFail() function.Īt the first line, we are querying a user by its ID using the findOrFail() function. NOTE: This function will only be available if you are using Laravel on version 8.58.0 or newer. In case it returns false, an error message with status 400 will be responded.įinally, if the user got correctly updated, we render the updated user as a response. Otherwise, it would be necessary to use a try/catch block and the number of lines would be pretty much the same.Īs in the previous example, we have a conditional where we are calling $user->update() with the data that we want to update.
LARAVEL ELOQUENT UPDATE RETURN HOW TO
To make the most out of this change, we need to know how to automate the handling of the exception thrown by the findOrFail(). This function has a special behavior where an exception gets thrown in case the data for the given ID didn't get found. Enter fullscreen mode Exit fullscreen modeĪt the first line, we are querying a user by its ID using the findOrFail() function.
