Laravel Refactoring: Similar Controller store/update Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys what you're about to see is one free lesson from my latest course on laravel refactoring examples currently it's 11 examples published but it's an ongoing course so if you have more ideas what refactoring what type of refactoring i can show shoot in the comments below and you can purchase that for 19 single course or it's a better deal to purchase yearly membership and get 24 courses for 99 per year also including one year ahead so yearly membership means you will get for free everything i will release in 2022. now this example is about controller store and update methods which are really similar and how to refactor that in form request and how to shorten the controller itself let's take a look in this laravel refactoring example we take a look at a typical crud table with create and edit capabilities so you can add address and if we use fake filler chrome extension for example you can save and then you can edit the records and what if those store and update methods in the controllers are really similar duplicating the functionality pretty typical scenario that i can see in the routes web for example you have a resource controller which is cool and then in the controller itself we have in the store method validation for a lot of fields then creation of the records listing all the fields one by one and then in the update it's almost identical so validation in this case absolutely identical and then assigning those fields one by one with a different syntax it's not address create anymore but it's really similar anyway how can we improve it make it shorter and take those variables for example validations to somewhere else and that somewhere else will be a form request class but before we do that like in any refactoring you need to make sure that the feature will work after the refactor so for that we have feature tests in this case addresses test file in the tests feature folder test four scenarios so user can create address so we log in with the user and try to post to the addresses url and assert valid meaning no validation errors and a search redirect then we assert that there is a validation error cert invalid if we fail to pass some field for example and then similar to test for the update address in the success scenario and then also in the unsuccessful scenario so we create a fake address and then try to update it and there should be a validation error because some field is missing so we cover the basic scenarios and now we can try to refactor the code and see if the tests are still passing important thing with test is on which database are you testing so in phpunit xml i have sqlite with memory and as a demonstration we can open our terminal and run php artisan test and there will be four tests all pass everything is good now let's try to refactor so first request validate let's move that to the form request class which we will reuse in both store and update because in this case the rules are absolutely the same in some cases they may be a little bit different but in this case it's more simple so we do php autism make request let's call it address request because it's not store address request or update address request is just general address request then we use that in the store for example here instead of request we do address request now we click here in the php store and land inside of that file change authorized to true for simplicity and then in the rules we just cut and paste those validation rules into here and then we don't need to use request validate here at all and let's repeat the same thing in the update so change request to the address request and remove that request validation at all let's rerun our test and see if it all still green yep we are good so we moved one part the validation part to the form request and we have already shorter controller now these two things address create and update the tricky part here is that some fields in the database have different names than the input fields so c street name in the database but request comes with the street from the blade house number and just number also country id and country in ideal scenario these fields should be identical to the database for simplicity but we need to take care of this scenario and i will show you how also a few things are additional so in addition to the request there is user id set automatically to the logged in user and also token in the update we don't have the token we just assign the user id as well so we need to do some additional manipulation on top of request so generally if you didn't have user id and token you would be able to do something like this address create and then request validated validated means that all of the fields that are listed in that class will be returned and used as address create so if you don't specify some field here like for example is billing if you don't validate that then it will not be in the request validated array so okay request validated but also we need to add user id and token for that we can add a plus here there are a few ways how to deal with that syntax but one of the most simple one is plus and then you add two more variables so those user id and token would be something like this so we have a shorter address create instead of this long one but we didn't tackle the problem of different fields so number and house number how can we transform that and for that inside of the form request class we need to specify a function called prepare for validation so public function prepare for validation and we can transform this which is request we would use the merge method assigning new keys so we will have street name from what i remember let's check that out so street name so street name equals this street this is request actually in the form request class and then we have house number request number actually i will copy and paste everything so request becomes this and then the third different one will be country request country country this country now what happens then those three new keys will come to the validation and then we need to validate street name house number and country id let's auto format that with phpstorm okay and now we are sure that we are getting street name house number and country id and we can remove or actually let's comment that out for now all the longer version and we can rely on request validated let's rerun the tests php art is on test all still green so we didn't break anything so all that's left to do is to perform the same thing in the update method so update uses same address request and we can do address update request validated also we need to add user id but probably most likely in most cases user id should not change so i would challenge even to remove that line at all because if that happens then it's kind of overtaking someone else's record so i would challenge to do that so let's remove all of those and address save is not needed we only have address update as one line let's run the tests again php artisan test all green so this is how you can simplify the store and update method to use request validated and rules in form request class
Info
Channel: Laravel Daily
Views: 2,563
Rating: undefined out of 5
Keywords:
Id: W0jyphqYkdk
Channel Id: undefined
Length: 8min 2sec (482 seconds)
Published: Sat Dec 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.