‫All right.
‫So now we're ready to insert into the database.
‫And there's a few ways we can do this.
‫But what we ultimately want to end up with is something that looks like this.
‫So this DB query and then have an insert which will insert into listings, list all the fields and then
‫values and have all the placeholders, and then pass in new listing data.
‫Now we can't just pop this in here, because we only want to insert the fields that actually have data.
‫So we have to kind of build it dynamically.
‫So I'll actually leave that there.
‫Just for now, just so you can see what we're what we're building.
‫All right.
‫So we're going to loop through the new listing data.
‫That's that's an array that has the fields as the key like title description.
‫And then whatever is in the form as the value.
‫So let's loop through that.
‫So we'll say for each and pass in here new listing data.
‫And let's say as and we want to get the field or the key and the value.
‫So we can say field.
‫And value, so that will give us both.
‫And then let's above it just initialize a fields array.
‫So we'll just set that to nothing to just an empty array.
‫And then in here for each of the new listing data, we're going to then take fields.
‫And we're just going to add each field into this array.
‫So field.
‫Okay.
‫Now.
‫Outside of that, I want to take that fields array and turn it into a comma separated list, because
‫I want it to look like this.
‫So this part, this is what we're going to be creating right now.
‫And we can do that with the implode function.
‫Remember explode will take a string and turn it into an array.
‫And implode will take an array and turn it into a string.
‫So let's say fields equals.
‫So we're just going to kind of reassign this fields variable to implode.
‫Implode.
‫And we want to implode by a comma and a space.
‫So basically we want to take the array, turn it into a string of fields that are separated with a comma
‫and a space.
‫And we just need to pass in fields.
‫All right.
‫And we can check this out now.
‫So if we do let's say inspect.
‫Actually we can do an inspect and die on fields.
‫All right.
‫So let's try this out.
‫I'm going to come over here and just fill in some of these.
‫Just say test listing.
‫This is a test.
‫Okay, let's try that out.
‫We do need state.
‫And city is required as well.
‫Okay.
‫Save email is required as well.
‫Spelt that wrong, but that's fine.
‫All right.
‫So invalid parameter number of bound variables does not match.
‫So it's looking at this I believe.
‫Right?
‫Yeah.
‫Let's comment this out.
‫And then just refresh this.
‫It'll submit it again.
‫All right.
‫So now we have a string that looks like this title description salary.
‫So it looks exactly like this part.
‫Okay.
‫So we have that done.
‫Now we want to do the same thing for the values.
‫So let's come down here and let's say values.
‫Set that to an empty array.
‫And then let's loop through let's say for each.
‫And we're going to do the same thing.
‫So new listing data as field value.
‫And what we're going to do is convert.
‫Let's put a comment here.
‫We're going to say convert empty strings.
‫Empty strings to null.
‫So we can do that by checking.
‫Let's say if the value.
‫So if the current value and the current iteration is equal to an empty string, then let's set that
‫new listing data and whatever field that is set that to null okay.
‫And then after that if statement, we're going to then take values and actually make sure you have the
‫brackets here because we're adding two to that array.
‫And we want to create our placeholders which have a colon in front of them.
‫So we're going to do that.
‫And then we're going to concatenate on the field like that.
‫Now outside of the for each we're going to do the same thing we did with the fields.
‫We're going to say values set that to implode.
‫And we want to separate with a comma and a space and pass in values.
‫So now let's do the same thing here.
‫We'll inspect and die values.
‫Refresh that.
‫And now you can see we have all the placeholders.
‫So now we have this part of the query.
‫So the only thing really left to do is create the query.
‫So let's say query.
‫Equals and we're going to do.
‫Double quotes and let's say insert into listings.
‫And then we have in.
‫Parentheses.
‫We want all these fields which now are in that that fields variable.
‫So we can just pass in here.
‫Let's wrap this in in curly braces and say fields.
‫All right.
‫Now we just want to say values.
‫And then in parentheses we're going to put.
‫Our values.
‫All right, and then we can run.
‫This.
‫DB.
‫So we're saying this DB because remember up here.
‫In our.
‫So we initialize db here, and in our constructor we connect to the database.
‫So we can use this db to run any methods in that class.
‫And we want to run query.
‫So this DB query.
‫And then we're going to pass in our query.
‫And we're going to pass in our new listings data which contains the data.
‫So this right here is now this.
‫We just did it dynamically.
‫And now only the fields that have data should go in.
‫So let's try it out.
‫I'm not going to refresh this.
‫I'm going to go to the page again and properly fill it out.
‫So let's say post a job.
‫Oh, actually, before we do that, let's handle what we actually want to happen once it's inserted
‫into the database.
‫And I just want to redirect back to listings.
‫So in PHP we can use header.
‫So we can say header and then pass in here location colon and then slash listings or wherever we want
‫to redirect and then exit.
‫Now instead of having to do this whenever we redirect, what I will usually do is create a helper function.
‫So let's go into helpers dot php.
‫And let's go to the bottom here and we'll say.
‫This will redirect to a given URL.
‫And it'll have a pram.
‫Which will be a string, and it'll be called Earl.
‫And then return.
‫Will be void because it's just going to redirect.
‫So let's say function.
‫And I'm going to call it redirect.
‫And then it's going to take in a URL.
‫And we're going to call header.
‫And in here let's specify we'll use double quotes and we'll say location colon.
‫And then we'll put in our variable of URL.
‫And then we'll exit.
‫Okay, so now we can instead of doing this, we can just call.
‫Redirect and we can pass in slash listings.
‫So I think that that that looks a little better.
‫All right.
‫So let's add something in here I'm going to say front end web developer job description.
‫Um.
‫We'll just say this is a front end.
‫Physician working with react.
‫Annual salary will say 70,000.
‫Requirements.
‫Bachelor's degree.
‫Benefits 401 K.
‫And.
‫Health insurance.
‫Okay.
‫And then let's see.
‫Company name.
‫Address.
‫Okay.
‫We'll say info at test.com.
‫All right.
‫So let's go ahead and save and we get redirected.
‫And if we scroll down you'll see front end web developer.
‫All right.
‫Cool.
‫And if we click on the details page we can see that data.
‫Now one thing I forgot is the tags.
‫It looks like.
‫Did I forget that here?
‫Yeah I did.
‫So let's add a tags field.
‫So I'll go to let's see.
‫We want to go to our app views listings and create.
‫And I'll just grab this div right here that wraps the benefits and then go right under it.
‫And let's change the name.
‫Let's change here, here and here to tags.
‫Okay, let's reload the form page.
‫So now we have tags.
‫However we're not handling it.
‫So if we go to the listings controller and go to the store method tags is already in the allowed fields,
‫which is good.
‫Actually, I don't think there's really anything.
‫Yeah.
‫There's really nothing else we have to do here.
‫It should be included.
‫Because we have it here.
‫So yeah let's try that out.
‫So I'm going to just do a test.
‫Listing.
‫This.
‫Is a test.
‫Let's see what else is required at city.
‫And email.
‫All right.
‫So let's try that.
‫Oh let's add some tags.
‫We'll just say tag one.
‫Tag two.
‫And tag three.
‫Save that.
‫Come down here and now.
‫Tags are getting submitted.
‫Now, if they don't put tags because it's not required I don't want to show this this tags here.
‫So in the.
‫Let's see.
‫That's the.
‫What's this?
‫The home.
‫Now this is the listings.
‫So in listings and then index.
‫Let's go where the lie is that surrounds that whole tags.
‫And I'm just going to put an if statement in here.
‫I mean if you want you can use a ternary.
‫But I think that this is this is fine.
‫I think in this situation this is actually easier to read just because there's so much here.
‫So let's say if is, how are we going to do this?
‫We'll say if not.
‫Yeah, if it's not empty listing.
‫Tags.
‫Then show that.
‫So if I reload now.
‫Oh, sorry, I'm using the wrong format.
‫I'm using the associative array format.
‫I need to be using the arrow.
‫Like that.
‫So now if we go down to the front end developer listing, it has no tags, so it's not even showing
‫the title.
‫Now, I think one other thing we should do is make the salary required.
‫If not, then we have to do the same thing with the salary.
‫But I think that that's a good field to be required anyway.
‫So all we have to do to make something required is just go to this required fields and add to it.
‫So I'll put it right here.
‫Salary.
‫Should be in quotes.
‫Salary, comma.
‫All right.
‫So now if I were to.
‫Go to the list of form here and submit.
‫You'll see salary is also required.
‫All right, so I think we're good.
‫Oh, on the home page, we want to do the same thing with the tags.
‫So this right here.
‫This if if not empty I'll just grab that, go to the home view and just do the same thing here.
‫All right.
‫Cool.
‫And let's see on the details page.
‫Yeah, we should probably do.
‫That same thing.
‫So listings show.
‫I say when I say details, I'm using an interchangeably with show.
‫So where we have tags.
‫Um.
‫To the same exact thing.
‫Tags and strong.
‫Yeah.
‫So we can just kind of.
‫Replace that.
‫All right.
‫Cool.
‫So next we're going to make it so that we can delete a listing from the show page.
‫Now later on we'll make it so only the user that owns the listing can delete it or even see the delete
‫button.
‫But we don't have authentication yet.
‫So right now anyone will be able to delete or update.
‫So we'll start on that next.