Laravel 6 Beginner - e11 - RESTful Controllers Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back so this is episode three of restful controllers in level in the first episode we covered the first three actions of a resource controller in the second episode we covered the next three actions and then I left you with a bit of a cliffhanger and that was the delete sequence we touched briefly on the fact that you can only really submit posts or get requests through a forum so we had to fake the patch request the same way that you would have to fake the put request and then undelete well that needs to be a forum so it's not as simple as just adding a link we actually need to have a full-blown forum to be able to submit a delete request so let's go ahead and handle the delete request inside my show view my customers detail I want to add another button here for delete so let's go ahead and tackle that right now so back in phpstorm inside my show view here is that edit button so let's add a forum down here where the action is going to go to let's check this back back to /resources slash and then the resource name but it's going to be a delete request and if you remember we can do that using the blade directive for method so for method we'll go ahead and submit a delete request alright let's add that route and that's going to be the seventh and final verb so we'll say delete request to slash customers slash customer ID and that's going to hit the destroy method again the destroy method that's the action this is the URI and then this is the verb very simple and easy convention to follow okay let's keep hacking away at this back in my show view I've got this but now I actually need to submit the form to slash customers slash and then the customer ID so that's as simple as just grabbing their ID and then in here I'll just simply have a button that says delete and there we go so there's my delete button and if I hit the delete button it doesn't work just yet notice that I have a method here of delete which doesn't quite make any sense that's because my form is missing the method so let's go ahead and add a method here of post so again even though the final method is delete we still have to submit a post request then we need to fake it using this blade syntax and pass in the actual verb that we want because post and get are the only options we have for method again we talked about this in the previous episode alright let's give this another go I'll go ahead and delete this out of here and hit the delete button and now we get a page expired quick quiz what does this mean ding ding you should already be saying we forgot the CSRF token let's go ahead and add that in so at CSRF and then let's go back hit refresh hit the delete and now we do hit that however there is no destroy method inside my customers controller alright that's an easy fix back in my customers controller let's add the final one here for destroy as we've been doing all the way around we can use route model binding to grab our customer and all we need to do at this point is just delete them but we'll delete that record and then we'll return a redirect back to slash customers ok refresh customers there we go alright so that slash customers and there we go so now we see we only have two records left let's delete another test and there we go now we're down to just one record so just like that we've got all seven verbs from a resourceful controller implemented 100% and again I hope you see the flow of how this works and how restful controllers are going to make your life so much easier but now let's start a bit of refactoring there's a couple of things in my controller that are currently bothering me so let's fix one at a time the first one is duplication we've got this duplication right here for validation and then we've got the same exact thing right here in my store method what are the problems with this well if we add another field now we've got two places that we need to maintain this validation data and that's just not great easy fix for this is we can actually extract that into another method inside of our controllers now there are more advanced level features that you can use like form request let's keep it simple let's not bug ourselves down with yet another layer of complexity so I will cut out this request I'll actually delete that whole line there all together and let's add a new protected function down here you can make it protect it or even private it really doesn't matter I will call this validated data and from here I will return the same exact thing that I had in my store method so remember that this is going to either fail or it's going to return an array so at that point it's going to be an array either way otherwise it's going to fail and it's going to give us some sort of error in the front end we're not concerned with that okay let's go back to the store and now we don't have this data so we replace data with this and replace that where they call the validated data just like that and now we saved an entire line all together then we can do the exact same refactor in my update method I will go ahead and delete this data and instead of using I will actually put a call into this validated data and that will fix one of the issues that I have with this controller the next issue that I have is more of an aesthetic issue is that I'm actually using the fully qualified path to each of these classes notice that I have this app we can actually simplify this by using it at the top so we could say use app customer and now instead of having to do app customer I can delete the app part and just use customer and it cleans it up just a little bit so I will actually select all instances where I did use slash app and get rid of that and now it looks a little bit cleaner so there is our controller right there now one last thing that I want to fix is that when you create a new record right now we're actually going to slash customers let me show you what that looks like I'll go ahead and hit refresh let's add a new customer here for another customer with an email of email at email com add a new customer and it takes us back to the list of all the customers but I think that this should go to the show view this should be the customers details that's what it should really take me but right now we don't have the ID of the generated customer so how will we actually redirect there well it's actually quite simple the create method actually returns the newly created object for us so we can save that to customer and then we can redirect to slash customers slash and then the ID of the customer so customer ID all right let's check out that flow instead add new customer yet another one and then we'll put email an email comm add new customer and now we go to the customers detail page so that's just another quick refactor that I think it's going to be a nice win and there it is that is restful controllers we're going to continue to expand on the concept of restful controllers as time goes by but this will get you through the basics of how to generate an entire crud action in a single restful controller now I know I said I was gonna be finished right after this but I've got one more little trick that I think we can do to clean up a little bit and that is the following our form right the edit form as well as the create form remember that we copied the form from one place to another however we do have quite a bit of repetition because all of these fields are exactly the same now there is one difference between them and it is this value right here which right now we are defaulting to old in terms of the create form and in this one we do have the customer because at this point we would have a model that we are editing why don't we extract this into a form blade partial that we can reuse as much as possible so let's create a new file here and let's call it form that blade that PHP and in this one let me actually bring in the create form not everything right let's go ahead and bring the div tag and the CSRF so this section right here I will cut it out and let's go ahead and bring it into form like so and now my create form all I need to do is include this new partial and that is going to be under customer dot form okay this doesn't change right we can actually go back to the browser we go back here and your customer form is still there and then of course our edit form that's still there as well right so we haven't changed anything the form does need to stay outside because this is obviously going to be different from the create as it is from the edit so let's go ahead and keep that in there as well as this method right this is kind of special to this form right here however we can go ahead and instead of having this in here we can do the exact same refactor here we'll include that customer that form okay but at this point of course we have actually broken it because again we are defaulting to having old but we don't know if we're going to have old or not now if we don't have old what we could do is say okay you don't have old let's go ahead and use customer instead so say customer name and so with this small change we are going to try to find the old right and that's going to be the first thing we try to find now if we are unable to find old then we're gonna go ahead and grab our customer name now this is not going to work right away we're going to have to apply a different trick but let me show you what will actually happen so we'll put that in here now let's go back right here I'll hit refresh that one is still working and then if we go back into the add new customer okay so here it is now there is an undefined variable of customer basically what we need to do is we need to pass in a default customer just a blank customer that we can actually use so how would we handle that well let's go back to phpstorm and in my customer controller right now obviously the form that we're trying to fix is this new create form let's go ahead and just whip up a brand new blank customer and to do that we can say customer is equal to new customer but a little bit different than we've been initializing before but this will work and then in our data we'll just go ahead and compact customer at that point so basically what we have here is a totally blank customer and that will actually fix it for us and there we go now what's going to happen is that this customer because it doesn't have any in our create form whenever we're actually including the form and we're actually trying to arrow into name that's just equal to no and actually I could prove it to you I'll just die and dump customer arrow name and check this out but scroll down here there we go you see it says no right here and that's okay little smart enough to know that whenever there's a no we don't actually want the string of no you actually just want it to be a blank so with that we've actually refactored the form out of the edit and out of the create and that's it this functionality is exactly the same but if we needed to add another field I'll just create another email field here for example now that's it it's both in that form and in the Edit form obviously in our case I just repeated email so we'll have two emails but in the real world if you needed to add 25 different fields your edits and your create views stay in sync with one another so I think it's very useful to find small reef actors like that that you can perform because again it's gonna save you time and it's gonna keep your application nice and lean as little repetition as possible some repetitions sometimes is okay you need to know when it's okay and you need to have a certain threshold of acceptable repetition and then once it starts to bother you go ahead and refactor into something that doesn't need the repetition so with that we'll go ahead and wrap it up for restful controllers I hope you've enjoyed these three lessons they are very very important and I wanted to touch on them as soon as possible in your level learning because if you internalize the concept of creating restful controllers and sticking to the seven verbs it will make every application need to new just as simple as following the directions of restful controllers so with that we'll wrap it up for this lesson as always my name is Victor don't forget to subscribe and thanks for watching
Info
Channel: Coder's Tape
Views: 18,355
Rating: undefined out of 5
Keywords: laravel 6, restful controller, laravel controllers, laravel controller and view, laravel validation, laravel 6 validation, laravel validation rules, laravel validation tutorial, laravel request validation, laravel post request, laravel page expired, laravel page expired 419, laravel guarded, laravel fillable, laravel fillable vs guarded, laravel submit form, laravel form builder, laravel resource controller, laravel 6 resource controller, laravel 6 restful controller
Id: Bq_bS2SW1jY
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Thu Sep 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.