hey guys welcome back in this episode we're going to look at creating a custom upset method within
your controller the reason for this is to reduce code duplication to improve the code quality
to make your application more fluent and more readable for other developers in this example
i'm going to show you how to refactor the code in order to use the same absolute method for the
store and the update controller methods now if you look at your controller you can see that this is
typically what you'll see out in the wall you'll see a post being newed up and then the properties
are being set the save method's been called and then the post is being returned via a post
resource now for the update method you'll see that the each field is being checked to see whether
it's null and if it's not null then it is being assigned to a data variable with the same name
then that data array is being passed to the update method and then the post is being returned via a
post resource so what we're going to do in this example is we're going to combine the create and
the update logic and move it into the model now in order for us to confirm that we refactored this
code correctly i've set up three tests in order to check our refactoring the first test creates
a new post the second test updates the title of a post and the third test updates the description
of a post so now if we run php unit we can see that three tests have passed and six assertions
have been done i also use snapshot testing so if we look at the first snapshot you can see this
is the api response of the post resource the second snapshot shows the test for the updated
title and the third snapshot shows the updated description for the third test now in order for
us to combine the create and the update logic in the same methods within the model we need to
create some custom set methods within within our model for example setting the name we would create
a set name method which receives the name as an optional string parameter it then checks whether
the parameter is null or not and if it's not null it then gets set to the model property now the
reason for this is to allow this parameter to be null which could be from the update method in your
controller because we're using a patch method not all parameters are required so sometimes it would
be not because from the store method the field is required so it will always be present lastly it
returns the model meaning that we can chain these methods in our controller now i set up the same
type of set method for the description just one thing to keep in mind these set methods should
not be confused with mutators which follows a very similar naming convention pre-load of l9 laravel 9
and after you shouldn't have that problem now the last custom method i created is a commit method
this basically just calls the save method on a model and then return the model the reason for
this is because the save method doesn't return the model so we won't be able to chain it within
our controller method which i'll show you in a bit okay now back in our controller let's create
our custom absolute method like this okay so the first thing we can do is refactor our store
method and receive a post class via dependency injection that way we can remove this line of code
okay now in order to utilize this custom absurd method we can now return a new post resource and
then within the resource we can call this upset and then we can pass in the request and the post
model we received and then we can basically remove these lines of code and now we can do the exact
same for the update method and then remove all these lines of code okay so now if you look at
the upset method you can see that it receives a default request that is because both the update
post request and the store post request extends the default request that is why you can accept it
this way now for the post and the store method you'll receive a new post instance because it
is provided via dependency injection and for the update method you'll receive the post that
has been provided via right model binding and just before we run php unit again to see if this worked
remember guys if you like this video please give it a thumbs up also consider subscribing
if you haven't done so yet and turn on the notifications okay so now if we run php unit again
everything should still be green and there we go three tests passed six assertions have been done
and as you can see this is a lot shorter it's a lot cleaner it's more fluent and a lot easier
to read just one word of caution this absolute method in the controller should not be confused
with the absurd method or the built-in upset method within the model these are two different
things and that's it for this episode guys thank you for watching and see you in the next one