‫All right guys.
‫So now we want to make a delete request when we click this button.
‫And then it's going to call a method in our listing controller called destroy.
‫So we're going to do that.
‫But one thing that I noticed that I want to change, because it kind of bothers me, is that this route
‫right here, to get the single listing, we have a listing slash ID when all the rest are listings.
‫So I want to just stay consistent and kind of stick to a RESTful architecture.
‫So let's change that to listings.
‫Although if we do that, it's going to mess up a couple of things.
‫So one, this is this page will break because this details is now pointing to listing slash and then
‫the ID.
‫And you can see that up here.
‫So we just need to change the links in both the home view and the listing index view.
‫So let's go to home and let's go right here.
‫And just simply add an S to that.
‫And then let's go to the index listing index and also add an s there.
‫Now if I reload and I click details now it works again.
‫All right.
‫So I just wanted to to handle that.
‫Now we're going to want to have a delete request.
‫So let's add a new route let's say router.
‫Delete.
‫And it's going to be two slash listings, slash and then ID and again we already have this route here.
‫But this was a Get request.
‫This is a different it's going to be treated as a delete request.
‫We kind of have to spoof it because you can't actually make a delete request in the browser.
‫You can do it through like JavaScript and stuff like that.
‫But we have to kind of spoof it a little bit with a hidden input.
‫And we've done this in the past.
‫If you went through the database section of the course earlier, now what I want to call when this route
‫is hit is going to be the listing.
‫Controller, and it's going to be a method called destroy, which again is a common convention name.
‫So now let's make it so we can actually make this delete.
‫So in the show view so views listing show.
‫That's where this.
‫Let's click on one of these.
‫That's where this delete button is.
‫Or I should say delete form.
‫So this is it right here.
‫It has a method of post because we can't say method delete.
‫It has to be either post or get.
‫However we're going to add an input in here with the type of hidden.
‫And then the name is going to be underscore method.
‫And then for the value we're going to set that to delete.
‫All right.
‫So make sure you add that.
‫Now when I showed you this back in the database section, we also had to have a hidden input that that
‫passed along the ID.
‫But we don't have to do that because now in our routes we can pass in an ID param.
‫So no need to pass the ID through a hidden input.
‫The next thing we need to do is update our router a little bit.
‫So let's go into framework and router.
‫And we just need to check for that hidden input.
‫And if that exists then we want to use that go right under where we get the request method.
‫And let's say check for we'll say check for underscore method input.
‫So let's create an if.
‫And we'll first check to see if the actual request method is equal to post.
‫Okay.
‫And we want to see is set.
‫In the post.
‫Super global.
‫We want to check for underscore method.
‫And if that's there then we'll say override the.
‫Request method with.
‫The value of underscore method.
‫So then we'll just set request method equal to.
‫And I'm just going to pass this through string to upper.
‫And then we'll pass in post and the underscore method which could be delete.
‫It could also be put or whatever else.
‫We can close up the router and we can close up the rest of this stuff too.
‫And let's go into our listing controller and we want to add a destroy.
‫So down at the bottom make sure you're still in the class.
‫And let's say this will delete a listing.
‫And it'll have a pram.
‫Of it'll be an array and it'll be our params.
‫And then the return.
‫Is going to be void.
‫And did I add the pram to the show?
‫Let's see.
‫Show a single listing.
‫Yeah.
‫I want to just add that.
‫So right here, let's say pram.
‫Params.
‫Or array params.
‫Now why is that not red?
‫Because these should be red.
‫I think I need another asterisk here.
‫Like that?
‫Yeah.
‫So we need to do that if you want these to be colored.
‫So let's do that here.
‫And let's see.
‫So that's good.
‫All right, so let's create our destroy, let's say public function.
‫Destroy.
‫It's going to take in.
‫Params and that's how we can get the id.
‫Now before we delete we need to check and see if it's there.
‫We don't want to try to delete a listing that's not there.
‫So let's get the id from the params array.
‫So params id and then we're going to be doing a select statement where we have an id placeholder.
‫So we need to have our params array where we set the id to the id from this params that's passed in,
‫which ultimately comes from the URL.
‫Then let's get the listing.
‫So we'll say listing equals and we'll make our query.
‫So this db query.
‫And the query is going to be select all from listings.
‫And let's say where the ID equals.
‫And then we'll add our named parameter and then pass in the params.
‫And we want to fetch.
‫We're just fetching a single listing.
‫So it's not fetch all.
‫It's just fetch.
‫And then what we'll do is say if not listing okay.
‫So if the listing does not exist then we're going to use our error controller.
‫And we're going to use the not found method.
‫Okay.
‫And then we'll pass in custom message and say listing not found.
‫All right, and let's return after that.
‫Okay, now if there is a listing, then we want to delete it.
‫But just for now, let's do an inspect and die and say listing.
‫Okay.
‫So I'll come over to one of these listings.
‫So this is what this is the ID of one.
‫I'm going to click delete.
‫And now it gives me the actual listing of the the data that I want to delete.
‫All right.
‫So we have ID one user ID one title etcetera.
‫So now that we know we can get the listing and let me I just want to go to the one that I want to delete,
‫which is going to be this test one right here.
‫So if I hit delete, you can see the data.
‫And notice that any of the fields that weren't filled in are null okay.
‫Because of how we how we did the insert.
‫Now to do the delete, we're simply going to let's get rid of this inspect and die.
‫And we're just going to use query again.
‫So this DB query.
‫And we're going to pass in here delete from.
‫Listings.
‫Where the id again is equal to the named parameter id and then pass in params.
‫So this.
‫This query method is very easy to work with.
‫You know you could have separate methods like insert, select and all that.
‫If you want to do that, that's fine.
‫But I think that I mean this is good for this application.
‫So after we delete let's go ahead and redirect to let's go to slash listings.
‫So we're using the redirect helper function that we created.
‫All right.
‫So now just reload this and then we're on the test listing I'm going to click delete.
‫We go back to the listings page.
‫And now it's gone.
‫Now one thing I would like to happen is it to in addition to redirecting, I'd like it to tell us that
‫it's been deleted.
‫So in the next video we're going to get into something called flash messages.