Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e17 – Eloquent Route Model Binding & RESTful Controller – Part 3

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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

Course Source Code

Hit us up on Twitter with any questions or comments @codertape (

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue