Laravel 6 Beginner
Lesson 10 of 14 · Laravel 6 Beginner – e10 – RESTful Controllers Part 2 · Education
Laravel 6 Beginner – e10 – RESTful Controllers Part 2
[Music] let's pick right back up this is episode two of level six beginners restful controllers so in the
first episode we implemented the first three verbs of a resource controller now let's jump right in and start with the show view if you missed the first episode I highly recommend you check
that out because this series really dives deep in teaching you level so let's jump right to it the next step is to get this show view working and as you've probably already figured out we
can actually continue to follow this form now we're gonna run into a bit of an issue and that is that we need a variable so I'll show you how to actually have a dynamic variable in your
URL it's pretty cool but let's jump into my routes file and let's add this new route I'll copy the one from the top and this is going to be a get route as designated here and then it's going to
be slash and then the resource name then you have this curly bracket curly bracket right around that so we're going to go to slash customers slash and then curly braket customer in singular so we
have our resource name is customers plural and then we have customer as singular okay so what action are we going to hit we're going to hit the show action so in the show action what are
you supposed to do in the show action think of this page as the details page if you just created a customer you could click on it and it would take you to that customers page and it will show you
all of the details for that customer let's jump right in here and let's add this new method for show so the first thing we need to do is figure out how to get the ID of the customer because we
need to dive into the database again and grab a particular customer now we don't even know how to do that so there's going to be a lot to learn just by implementing this show view let's start
with how to actually generate these links if we are in the index method that is this one right here I want to generate a link that I can click on the name and that would take me to that
customers resource and again that takes shape of slash customers slash and ID right in that case it would probably ID of one because this is a very first record that we created so let's jump
back here and figure out how to get that done I will actually wrap this name inside an anchor tag and again will output the name inside the anchor tag so we need to go to slash customers slash
and now we need the ID well no problem we can use blade again and we can say give me the customers ID now the reason why we used that curly braket notation in the route file is to let level know
that this is going to be a dynamic URL and it's not actually looking for the exact string of customers but rather we are looking for whatever comes after the slash so we're looking for a slash
customers and then a slash and then everything else is just going to be put in a customer variable for us but we actually need to accept it and how do we accept it right here right in the show
view we can say okay give me the customer now this customer right here matches up with customer right here and to prove that that is actually working I will simply died and dumped customer at
this point okay so we've done all the light work if I hit refresh now these are links and they are clickable notice down here in my browser when I hover over one you get one and then two and
then three okay so now if I click on that there's my die and dump and we see one now if that was anything else that would just pass that through so it's not restricted to just something they'll do
keep that in mind but we'll say one one is the ID that we're actually looking for okay so how do we go into the database and grab this record well we haven't really learned how to do that
just yet let's do it let's just dive right in and actually make it happen we'll say customer equals and then we'll grab our model again we'll say app customer and I
want you to find the customer however I want to name this customer and this is customer as well there's not really going to work so you have two choices here
you could either rename this one to something else or we can rename the actual route to maybe customer ID why don't we do that we'll say customer ID let's go back into my web and actually
rename this to customer ID okay let's see what happens now I will die and dump my customer at that point and let's see if we can get that back from the database hit refresh and sure enough
you see that we get an instance of customer and if we open the attributes we have our customer how simple is that pretty cool stuff so ok so now that we have that well we need to follow the
same stuff we need to go ahead and return a view and that's going to be inside the customers directory and this is going to be that show ok let me go ahead and create that right now well add
a blank file for show that blade that PHP inside of here again will have an h1 tag and we'll say customer details and then very quickly let's go ahead and add a strong tag here for name and then in
ap tag let's go ahead and output out my customer name and then we'll do the same thing for the email ok P tag will output out customer email and again we need to pass in the data
into our view oh go ahead and hit compact with customer okay let's hit refresh and sure enough we have our customer details it would be nice to maybe have a back button let's do that
now very quickly let's add another anchor tag here to slash customers and we'll say back maybe we'll put a less than sign whoops let's actually put that in a div that way it has its own line
there we go again it doesn't look very pretty but it'll work so we can go back we can go forward into each one of these customer details and just like that we've been able to do that but now we
have a bit of a problem and you may have not realized this just yet but what if somebody goes in here and types in ten customer with ID of ten obviously doesn't exist but when I get that I get
this huge blow-up and what is actually happening is that level is continuing and trying to load this view even though it doesn't have enough data because there is no record by the number of ten
now one quick fix that we can do and this is that instead of find we can do find or fail find or fail if it's not able to find the record will give you the proper not found 404 HTTP error code
and that's really what you're looking for let me hit refresh and now we get a 404 not found very easy now there's a bit of magic here and I don't want to hold any longer because I want to show
you what route model binding is this is a bit of an advanced topic but check this out we can actually get rid of this entire line all together and I'll show you exactly how this fetching of a
single record is such a common thing that levo has something called route model binding where it basically binds or glues together a model into a particular route and we can do that by
just simply following a very basic naming convention I will go ahead and delete this line completely out of the equation all together next I will rename this back to just
customer I will take off the ID off of customer and I'll go into my route files and make the same change so now my route only has a variable of customer simply by putting in our model in front of
customer level will fetch that record for us automatically but we just have to say app customer and that's it from here on out level will actually fetch this customer for us don't believe me let me
hit refresh and we still get that 404 but if we go to ID of one we still have our customer how cool is that this saves you a ton of time the letter will go out and try to fetch the record for us again
if it doesn't find it it will give us the proper 404 response now in order for this to work a little bit of a caveat is the naming convention has to be there this name right here has to match this
name right here it doesn't matter what it is it could literally be something as long as the name here in your route matches the name of the variable right here if it doesn't then it's not going
to work just to prove it let me actually go ahead and change this everything over to something now hit refresh and notice that everything still works now if I was to actually undo this change and maybe
keep this ask customer but in my route file is still called something it doesn't work the name absolutely has to match otherwise it doesn't work so customer right here in your routes
file has to match with customer the variable name that you use inside of this but that's how that works very easy to use and extremely helpful so up next we have the edit route this is the route
that you would hit if you actually want to go ahead and edit a record so let's go ahead and just implement that it's going to be a get route and it's going to be basically the same route we had
for the show view but we're gonna tag on slash edit and then that's going to hit an action of edit okay back into web I'll copy this line and we're gonna do slash edit at the end
of this and we're going to hit the edit method inside of our customers controller so inside of my customers controller let's add a new edit method and this is a very simple method all it
needs to do is go ahead and return a view what we do need our customer so we'll fetch that the exact same way but one thing at a time we'll say this is going to be customer dot
edit and then we do need our customer so we can grab the exact same logic that we have here and it's still going to work because remember we have this variable name here that is named exactly the same
thing as it was for our show view so in our edit we'll actually have our customer so all I'll need to do at this point is just pass that through to my view now the reason why I need this is
because I need to display the current users information or the current customers information rather that's why I need the actual data ok let's create that view customer new file edit that
blade dot PHP h1 tag will say edit customer details and then in here we need to have basically the same form that I have in create I will go ahead and duplicate
this but like I said I want to actually factor this into us cleaner code as we possibly can I will go ahead and grab the exact same form and bring that into my edit view temporarily this is going
to hit a different action and we'll talk about where it's gonna go in just a second but for now let's go ahead and just put a hash but we're going to have our name but instead of old I need to
actually output out my customers name and then same thing for email we need the one that is actually being passed in through our controller remember in our controller we're passing our customer
straight through so here all we need to do is just output that into each of those fields now the button saying add a new customer doesn't make any sense we'll say save customer how about that
and now we come back here we'll need to add an edit button somewhere in my customers detail customers detail is the show view so let's add a new div here with an anchor tag for slash customers
and then I need the ID of the customer again now luckily for us we do have customer ID so it's as simple as that slash edit and we'll just name this edit but now we have an edit button when we
hit that there we go we can actually do that so when we hit this safe customer where is it gonna go well back to the book again we're going to submit a put or a
patch request to slash resource / is going to hit an update method this may be a bit confusing at first the fact that there are two verbs in one route now this is a choice here I personally
like patch but put or patch they are just about the same thing there are some very very subtle differences and in most cases the proper one actually is put but I like patch because patch sounds more
like what we're doing we are patching a record putting a record actually sounds like an insert to me but we'll go ahead and use patch but keep that in mind that you can use either one it really doesn't
matter alright so back to phpstorm in my web browser file i'll add another route here and like I said I will use patch and we don't need that slash edit and that is going to hit the update
method again so a patch request to this URI and that's going to hit the update action notice how easy this flow is and that's because of this naming convention and the restful controllers I promise
you if you stick with this stuff it'll make development extremely easy and simple but let's keep going with this patch request so what do we need to do in the patch request well it's going to
be fairly similar to inserting a new record the difference is going to be in this line right here we're not gonna create a record but rather I need to update an existing record so let's take
it one line at a time I will copy every single line out of my store method and let's create a new update method right here and I will simply paste that in now in terms of validation we are going to
have to do some refactor because right now we have this duplication I don't love it we're gonna clean it up I promise but now in terms of my customer how do I grab my customer well we
already know how to do that we can simply use route model binding again in my update field and at that point I will have a customer so how do I update a customer that's actually quite simple as
well we'll say customer will call the update method on it and to the update method we'll pass through our updated data cool all right let's give this a go I will hit refresh here and when I save
customer it doesn't let me and you may be wondering what's going on you're using a patch request right and the first instinct might be to jump into your edit view and change this right
here to patch right is that going to work let's find out I'll erase that again save customer and what is all of this that's crazy actually patch doesn't work it doesn't
exist in the documentation for the form actions the methods that you can use are either get or post so how do we submit a patch request to do that level has a cool blade syntax that you can use which
is at method to method you pass it in what you actually want it to be so you're basically faking patch now the reasons that we have to fake these different actions are a little bit
outside of the scope of this particular lesson but if you want some extra reading go ahead and research why you can only use get or post in an actual form remember I had erased it so this
needs to be slash customers slash and then the ID of the customer the customer ID let's give this one more try and this time we do get validation errors so how cool is that now let's go ahead and
actually update this I'll put my last name in save customer and there we go it is updated right in here pretty cool we are making great progress so the very last thing we need to cover is the
destroy which is quite simple to implement but this does require a little bit of magic because we still have this verb of delete it's not as simple as just putting in a link but we'll leave
that for part three of restful controllers I'll see you then
RESTful Controllers help guide your development process and give you guidelines on how to implement your CRUD actions. Let’s take it one step at a time implementing and adding the concept customers into our application.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Laravel RESTful Controllers
https://laravel.com/docs/6.x/controllers#resource-controllers
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
About This Course
If you have been looking to learn Laravel, there hasn’t been a better time. The framework reached an important point in its life, with the release of Laravel 6. There has never been more demand for Laravel developers than now. In this series, we are going to get you started in your path to becoming a Laravel Artisan. Let’s get right to it!