Laravel 5.8 – From The Ground Up
Lesson 17 of 52 · Laravel 5.8 Tutorial From Scratch – e17 – Eloquent Route Model Binding & RESTful Controller – Part 3 · Education
Laravel 5.8 Tutorial From Scratch – e17 – Eloquent Route Model Binding & RESTful Controller – Part 3
so what's next so we have our index create store and show method so the next step in a resourceful controller is to have the Edit so I will show you how to
implement the edit using a lot of the same logic from create because remember that they create method has the form that we are looking for and this is the form so if we could reuse this form it
would be even better because then we don't have to maintain two forms so we're going to do a little bit of refactoring into partials to make that work so let's get right to it I'm gonna
close everything up again and let's open the create that blade PHP file and this is the form in question so I'm gonna leave the form itself in this create method and the reason being is because
the action will change from one form to the other but this is the part that we can use here so I will take all of this and I'm gonna cut it out and then we're gonna put this in a partial so let's say
include customers dot form so let's create that now in my customers directory new file form dot blade dot PHP so we have this and if we go back it's still working
now this CSRF we can actually move this into form let's put it here at the end there we are refresh still working all right I'm gonna save as this create method and I'm
gonna change it to edit remember that's our next step let's go back it is this is the step that we are on we're on the edit step so edit that blade I'll save that
so we'll say edit details for and then of course we're gonna need a customer so we know we'll have a customer we could put their name in there we'll say edit details for and let's put in the
customer name again okay and then the button needs to say safe customer and where is this action going to be let's go back to our guide and the action is gonna be to the update
method right so the Edit is the form that we're going to change to make the changes and then we're gonna put or patch to this update method so we know the URI is gonna be the resource name
and then the resource ID so let's change that now that's gonna be customers slash and then we need the customer ID that's what we're going to be posting to but you can't change this and just put patch
right that doesn't work in order for us to be able to do anything but get or post we have to use another layer of our helper and the letter of a blade helper is gonna be at method and Method is how
laravel gets around the fact that HTTP forms can only have a post or get which wouldn't allow us to do something like patch or put and that's what it's calling for is calling for put or patch
I like the word patch for me patch means that you're patching something you're editing something the verb put almost feels to me like it needs to insert something like we are putting something
in the database it feels like an insert so for me the word patch works better but you can use either one you can either use the put or you can either use the patch I recommend the patch just
because to me it makes more sense but it really makes no difference at all so with that being said our method is patched so what is going to happen is this form will actually post using the
method of post but once it gets to laravel level is gonna recognize that we actually want a patch request so let's make that route right now let's go to web routes and let's add another one and
this is not going to be a get this is going to be a patch so we're gonna patch customers customer and that's gonna go to let's go back here that is gonna go to our update method so we'll change
that now and of course we need one more route for edits so let's add that one now so it's gonna be customers and then our ID and then / edit and that's gonna go
to the Edit method so we have the get edit method right here and that's belonging to this and then we have our patch method that will actually save and update our record so far so good so we
can close all of this let's work on our controller as good of our customers controller and down here we need to add another function for edit and another one for updates so let's add those
quickly edit and update both of them will accept a customer and we're gonna use route model binding again so we know once we hit this edit we will have a customer available to us because the
laravel using this type int will automatically fetch that record for us so that's very convenient in the edit method all we need to do is return a view of again convention customers dot
edit and we're gonna compact customer and then we'll work on our update method but let's get this working first so in my show I want to add a link somewhere over here let's say down here we'll add
a link to customers / then the ID customer ID / edit and we'll call this link edit just to keep it simple let's go back here customer list let's pick one and now we have a link to edit so
edit and so now we're missing companies which makes sense if you recall going back to our form here add new customer we have this drop down menu here where you choose what company this customer
that you're adding is associated to and we're giving that data to that method using this companies all let's go ahead and pass that in let's do the same thing in our edit menu we're passing the
companies and we'll go ahead and compact companies alright let's go back and try that one more time John Doe edit and there we are so we have the Edit
view for John Doe however none of the data is filled out we definitely need to fill out that data so where is that data coming from well at this point we've been using old for that but we can say
use old or let's use customer name let's go back here hit refresh and there we go so now we have John Doe coming through let's do the same thing for the email so as you can see we have Jane Doe and we
have our email we will handle the status and company in another lesson but I want to start working on being able to save and update the name and email so let's visit our customers controller and let's
go to this update method the same as the create method we need to validate our data first this is no different than the create method so we need to revalidate this data this data right here we need
to revalidate it so I will actually copy this and once we have it all working we can extract that to its own place so for now we're just going to validate that we have a name and an email so then we're
gonna take our customer and we're gonna update it using this data that we just acquired so customer update data and data again is gonna be our validated data and then we can simply return a
redirect to customers slash and let's give it the ID so customer ID that way we go back to the detail page alright let's give it a go Jane Doe changed safe customer and there we go Jane Doe
changed let's change the email now another test com one two three and there it is but we're still validating so if we hit edit and for example delete it the email and says nope the email field
is required and no it puts back the original field back on so let's do one more thing let's go to customers list and add a new customer and everything blows up and the reason
for that is because in this view remember we're sharing the forum we have this old or customer name but customer may not be set so I'm gonna leave you on this as a bit of a cliffhanger for this
episode but try to figure out on your own how could we make this work how could we have old and a customer name in this value so that we can continue to reuse this form for both the create and
the edit so try to find a solution for that and in the next lesson we're gonna take a look at it and see how we can fix it
In part 3 of our RESTful Controller, we are continuing with the Edit and Update methods of our controller. Follow along as we get this implemented on our project.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
https://laravel.com/docs/5.0/controllers#restful-resource-controllers
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!