‫Okay.
‫So now we want to be able to submit this form.
‫And we have a route already that is handling a port request to listings ID.
‫So that's where we want the form to submit to.
‫Now obviously we can't do method equals put.
‫So we have to add our hidden input okay.
‫So let's go to the edit view and up to where the form tag is.
‫And we also have to change the action because it's going to go to listings slash and then the ID.
‫So here let's.
‫Put the listing ID okay, so it should be slash listing slash ID and then directly in the form we're
‫going to have an input.
‫It's going to be a hidden input that's going to have a name of underscore method, and it's going to
‫have a value of put.
‫So that will basically it'll it'll show to the router as a put request.
‫So now let's go to our controller.
‫Our listing controller.
‫And we're going to create our update.
‫So make sure you're within the class.
‫And let's see.
‫This will update a listing.
‫And for Param, we're going to have an array of params because it's using the id in the URL and then
‫return void.
‫Voice void.
‫Say public function update.
‫Take in params.
‫And for now, let's just do an inspect and die of the params just to make sure the form is actually
‫hooked up.
‫So reload the edit page and then just click save.
‫And now you should see this.
‫All right so we know the form is hooked up.
‫Now with the update it's going to be kind of a combination of the show method and the create method.
‫So why don't we start by fetching the listing we want to update.
‫And we can actually copy what we have in edit, because that also fetches the listing.
‫And this chunk of code right here we use quite a bit.
‫So we'll probably refactor this later and have a function that does this.
‫I think we're using it in three different areas, but we'll save that for later.
‫So let's just copy this stuff here.
‫Getting the ID from the param, adding it to the params array to pass into the prepared statement,
‫running the query, checking if it exists, showing an error if it doesn't.
‫So we just want to actually I'll leave that there and just paste this above it.
‫All right.
‫So that's kind of the first part of this.
‫Now we're going to be inserting it into the database or updating you know updating the fields that that
‫we change.
‫So we need that allowed fields array again.
‫So I'm just going to go up to the store method and grab that.
‫So a lot of the stuff and I said a mix of the show and create, it's actually the show and the store.
‫The create is really nothing but.
‫Loading the view.
‫It's the store is what actually does all the work when the form is submitted.
‫So let's grab that.
‫In fact, we can probably grab.
‫Um.
‫Yeah.
‫We'll just we'll just get this and then I'll type the rest out because it's quite, quite different.
‫So let's see right here.
‫We're going to have our allowed fields.
‫And then after that let's create an array of updated values.
‫Okay.
‫And we'll just set that to an empty array.
‫Then we're going to loop through the allowed fields.
‫So we'll say for each.
‫And allowed fields as field.
‫And then we want to check if is set.
‫So here we're just we're just passing in the actually, you know what?
‫Instead of doing this, let's go back up to store.
‫And we can do it like this with the array intersect key and array flip.
‫So this is just making sure that we only we only have an array of the allowed fields.
‫So we could do it like that loop through and you know, but instead we'll do this.
‫We just need to change this variable here to updated values.
‫Okay.
‫And we can check that out.
‫Updated values.
‫And if I were to submit this form.
‫You can see we have our data.
‫Now, if I were to take the title out of allowed values.
‫And you can see the titles there.
‫But if I reload and resubmit now, the title is not there because it's not in this array.
‫So it's not allowed.
‫But I do want it to be allowed.
‫So I'll put it back.
‫All right.
‫Now.
‫Let's run the sanitize function on every item.
‫So again we're going to say updated values.
‫And we're going to set that.
‫And we'll use array map which will apply a function to every item.
‫And the function we want to apply is sanitize which is in our helper file.
‫And then updated values.
‫So that takes care of that.
‫Now we want our required fields.
‫So let's say required fields and set that to let's see we're going to have our title.
‫Description.
‫Salary.
‫What else?
‫Email.
‫And then city and state.
‫So those are the required fields.
‫Then we're going to have our errors array.
‫So errors.
‫And we want to loop through the required fields.
‫So for each.
‫Of the required fields as.
‫Field.
‫And in here we want to check to see if they're empty.
‫So we're going to have two conditions.
‫We check here.
‫So one is just empty.
‫And then pass in our updated values with that specific field.
‫And then we also want to say or and then not our validation class string method.
‫And pass in here again updated values with the field current field.
‫So if either one of those match are true then we want to add it to the errors array.
‫So errors and have the field as the key and then as the value it'll be a message.
‫So we'll say you see first pass in the whatever the field is concatenate onto that.
‫Is required.
‫Okay.
‫So if we want to check that out, say errors.
‫And if I reload that, resubmit.
‫We don't have any errors.
‫But if I go back to the form and let's say forget the title.
‫Then there we go.
‫Now, this is kind of this validation isn't going to be run as much, because you're probably not going
‫to have missing fields because the data is already there.
‫So whatever you save it as is what it's going to be saved as.
‫So whatever.
‫You know, like when you come to the page, the titles are already there.
‫So unless you delete it and then submit it, it should be fine.
‫But we still want to have some validation.
‫So now let's come down here and say if let's say if not.
‫Empty.
‫Errors.
‫So if that array is not empty, it means we have errors.
‫So then we want to load the view.
‫And we're going to load listing.
‫Slash edit.
‫And we're going to pass in.
‫Let's say listing.
‫And we want to pass in the listing from the database because in the form.
‫Uh.
‫Let's see.
‫So in the form for our values.
‫We have.
‫The.
‫We're using the object fetch.
‫Right.
‫So listing arrow description etcetera.
‫So we need to pass in the data from the database.
‫And then let's.
‫Set errors.
‫Two errors.
‫Yeah.
‫And then we'll just do exit.
‫So else then we want to.
‫I forgot my semicolon.
‫So else then we want to submit to database.
‫And for now we'll just do.
‫We'll just say success.
‫All right.
‫So let's try that out.
‫If everything is full, then I get success, right?
‫But if I'm missing something and then I click save.
‫Well, I must have.
‫Yeah.
‫This should be listings.
‫Edit.
‫Okay, so if I forget something and click save, then I'm going to get that error and then whatever
‫is in the database will be will be put back there.
‫So now that we have that done, we need to actually submit it to the database.
‫So let's get rid of this inspect and die.
‫We can also get rid of this one down here.
‫So we have the update values right this array here.
‫Actually I meant to call it update values not updated.
‫So let me see.
‫Let me just select.
‫I'm just going to do a command or control D to select all of these.
‫Just go through and make sure I get them all and then change it to update.
‫Values, or you could call it values to update.
‫It doesn't really matter.
‫I'm just being kind of picky here.
‫All right.
‫So and let's just make sure this still works.
‫So if I miss something.
‫If I keep that in there.
‫Okay.
‫Just refresh that and then edit.
‫All right.
‫So let's start off here to submit to the database we're going to.
‫We need an array for the fields as well or the keys.
‫So title description etcetera.
‫We have the values.
‫So let's create a variable called update fields.
‫And we're going to set that to an empty array to begin with.
‫And then let's see, we're going to loop through the keys of the values and add those to the update
‫fields, because we want the keys in that array.
‫So let's say for each.
‫And the way we can do this is we can use array underscore keys, which will give us an array of the
‫keys and pass in update.
‫Values and say as field.
‫And we can see what that gives us.
‫So we'll inspect each field.
‫Right?
‫So if I do that, I reload this and then I submit.
‫You can see now we have title description salary etcetera.
‫So in that array we'll have, we'll have these fields or these keys.
‫Now we want to set our placeholder in the query like we want to have title equals the placeholder which
‫would be colon title.
‫So we want to do that here and put it in the update fields.
‫So let's say update fields.
‫And make sure you add your brackets here because we're going to basically append on to that array.
‫And then a string that will be the fields I'm sorry field equal.
‫And then colon whatever that field is.
‫And those will parse since they're in since they're in double quotes.
‫In fact let's just.
‫Add these around.
‫Like that.
‫Okay.
‫And we can check that out as well.
‫So outside of the for each let's do an inspect and die and update fields just to see what that looks
‫like.
‫So you should always this inspect and inspect and die function is very helpful.
‫Or just vardump or whatever you want to use, but you want to check to see while you're coding, check
‫to see what's in these in these variables.
‫That really helps just in general.
‫So let's reload this.
‫And now you'll see we have an array that has title equals title description equals description.
‫Now in our query obviously we don't want this to be an array.
‫We want a comma separated list of this stuff here because that's what our update query looks like.
‫So what we'll do is outside of the for each let's use implode to make it a string.
‫So now we'll say update fields.
‫Set that to implode.
‫And we want to separate them by comma space and then pass in update fields.
‫So now.
‫Um.
‫What's going on here?
‫Oh, I forgot, comma.
‫All right, so now again, let's see what's in there.
‫So inspect and die update fields.
‫And now it should no longer be an array.
‫It should be a string.
‫Like that.
‫So we have that part of the query.
‫So again just like with the with the insert we're just dynamically building this query.
‫So next.
‫Let's say update query.
‫And we can now set that to let's say update.
‫Listings and we want to set.
‫Update fields where the ID is equal to colon ID, and then we'll take a look at that query.
‫So update query.
‫Check it out.
‫Reload.
‫There we go.
‫Update listings set title to title, description to description, and so on.
‫And then at the end where id equals ID.
‫So now we're ready to actually run this query with our query method in the database class.
‫So let's go under Update Query and let's call this db and query.
‫All right.
‫Let's see.
‫So we're going to pass in here the update query that we just created as well as the update values.
‫All right.
‫Once that's done, then let's create a flash message.
‫So we'll say session and let's do a success underscore message.
‫And we'll set that to listing.
‫Updated.
‫And then let's redirect.
‫So we use our redirect helper function.
‫And we'll pass in here slash listings slash.
‫And I want to go to that listing.
‫So we'll add on to that ID slash ID and we'll get rid of this.
‫All right.
‫So now let's just go to an edit page and let's try to change something here.
‫So I'll change the address.
‫Let's change it to 123 Market Street.
‫And this might not work right now I might have screwed something up or forgot something.
‫But we'll try it out.
‫And we did.
‫Or I did screw something up.
‫So undefined.
‫Constant update query.
‫Oh.
‫Simple mistake.
‫Okay, let's reload to try it again.
‫Invalid parameter number number of bound variables does not match.
‫All right, so there's something wrong with my query.
‫Shh.
‫Let's see.
‫Um.
‫Huh?
‫Oh, you know what?
‫I think we need to add the ID here.
‫So let's try doing this update.
‫Yeah.
‫Let's say update values.
‫ID and set that to ID.
‫To reload.
‫There we go.
‫All right, so let's take a look.
‫The address.
‫You know what?
‫We don't have the address on the page.
‫Which is fine.
‫I mean, if you want to put it on the page, you can.
‫But the main reason I had an address field is if you wanted to add some kind of map feature.
‫So let's try to edit something else and we'll see if it updated here, which it did.
‫123 Market Street.
‫So let's change the salary from 70,000 to 80,000 and save.
‫And there it is, 80,000.
‫Now, we're not seeing the error message.
‫I'm sorry, not the error.
‫The flash message here, because we didn't account for that in in the show view, which is what we're
‫looking at.
‫So what we can do is copy from what do we have it in the create.
‫Whether we have the message.
‫Flash message.
‫Was it index?
‫Here it is.
‫All right.
‫So it's in the the index.
‫The listing index.
‫So wherever you want to show flash message you want to make sure you have that.
‫So let's go back to the show view and let's see where do we want to show it.
‫We'll put it like I guess above everything still within this this big white the card.
‫So let's click Edit save.
‫There we go.
‫Listing.
‫Updated.
‫Cool.
‫All right.
‫So we're now able to we have Crud functionality.
‫We can create read update and delete.