Bootstrap Grid Layout0:00
Welcome back. So now on the right-hand side we're going to add a little form to contribute a link. So why don't we do this. Let's grab all of this and wrap it within a div with a class of col-md-8. Now by default we're using Twitter Bootstrap here, so we get some easy grids out of the box. Okay, next below it we'll do col-md-4. These should add up to 12. And then we'll add another one for ContributeToLink or something like that. Okay, good. And then finally, I think, it's been a while since I've used Bootstrap, but you've got to wrap it within a row, right? So we can grab all of this indent, and that should bring the margin back, and it does. Okay, so here we get a little form. So let's come down, and I don't know, can we get away with a Bootstrap panel? Let's do panel, I think it's panel-default. And now I think we need panel-body. And then can we set up a form here? I think.
Build Contribution Form0:48
can we get away with a Bootstrap panel? Let's do panel, I think it's panel-default. And now I think we need panel-body. And then can we set up a form here? I think. So I know we'll need a section to choose a channel. We need a title. Let's add that real quick. Title. And then maybe a placeholder that says, what is the title of your article? Let's see what that looks like. Yeah, there we go. So add a title. Next we need a link. So let's do another one for that. Link. And the placeholder for that one will be, what is the URL to your article? Or let's just leave it, what is the URL? All right, give it a refresh. The title, the link. Now you can see, really, if you want, we could probably get rid of those labels and just use the placeholders. And I often do that. Finally, we need a button, right? So let's do, I don't think I have a button field here.
Submit Form to Route1:40
we could probably get rid of those labels and just use the placeholders. And I often do that. Finally, we need a button, right? So let's do, I don't think I have a button field here. So we'll change this to a button. And we'll say contribute link. And the classes are button and maybe button primary. Yeah, there we go. Okay, so I'm going to leave off the channel just a bit more. I think we're going to dedicate a whole lesson on setting up the channels, integrating it into the form, making everything dynamic, all that stuff. So for now, let's figure out what should happen when we contribute the link. Well, that should submit a POST request. So method is POST. And the action will be to /community. And don't forget, a handful of lessons ago, we set that up here. So we're all set. Once we submit this form, we're going to take all this data and send it to or post it to /community, where we can then
Add Store Method2:31
a handful of lessons ago, we set that up here. So we're all set. Once we submit this form, we're going to take all this data and send it to or post it to /community, where we can then fetch it, validate it, do whatever we need and throw it in the database. So let's do this. Let's tack on our CSRF field to start that hidden field. And yeah, we know there's going to be some errors, but we can at least try it out. My article, and then some link there. Okay, so immediately it fails. CommunityLinksController store does not exist. Oh, oh, yeah, duh. Of course, that doesn't work. If we go back to I just said on a screencast. Yeah, of course, we need a store method here. Okay, so now why don't we fetch the request. So use Illuminate\Http\Request. And I don't even think we need that one there. Okay, so receive the request. And now, if we didn't do any validation, we could do something like this CommunityLinks create, and then pass through all of the request
think we need that one there. Okay, so receive the request. And now, if we didn't do any validation, we could do something like this CommunityLink::create(), and then pass through all of the request data. And then here, let's set our fillable fields. So this prevents the user from trying to do anything funny, or explicit about the fillable fields, so they can update the ID or some column that we don't allow them to. So if you need a recap, so here, the user_id, we will do automatically. So we want the channel_title, channel_id, title, and the link for now. And we'll see how that goes. Okay, so yeah, the form request data comes through, we pass that through to the create method, which should persist it. And why don't we just redirect back for the time being and see how it's going. So we come back, I'm going to contribute the link. And we get a constraint. Okay, so we would expect this, right? We're trying to call CommunityLink.
Set Authenticated User4:16
back for the time being and see how it's going. So we come back, I'm going to contribute the link. And we get a constraint. Okay, so we would expect this, right? We're trying to call communityLinks create, but we didn't specify the user. Now, what a lot of people do, let me show you. I normally wouldn't recommend this at all. But some people will do like a hidden input. And the value will be the ID of the user or something. And then they fetch that from the form. And then they set the name to user_id. Once again, in general, I would recommend against doing this just because it would allow somebody to manipulate this, you can't assume that the form will not be tinkered with. So instead, most of the time when I'm working with a user or user_id, I like to be in full control of that. So instead, yeah, maybe we could say, get the authenticated user. And we could say, contributeLink, you know, just add a relationship there that will do it for you.
control of that. So instead, yeah, maybe we could say, get the authenticated user. And we could say, contribute link, you know, just add a relationship there that will do it for you. Or once again, yeah, you could just say something like $request $userId equals auth()->id(), any way you want to do it. Or let's keep going. What's another thing you can do? This is for me, this is the fun stuff is figuring out how you want it to feel and how you want to design things. So a third option would be, maybe you add my kind of a setter here. So communityLink from the authenticated user, or maybe let's just grab the user there. And then we could say contribute, and then we pass through the $request data. That would be an option as well. Okay, so if we wanted to do that, what might that look like? Well, as I see it, we have a static method called from, and that would accept the user. And then here, what would we do,
Okay, so if we wanted to do that, what might that look like? Well, as I see it, we have a static method called from, and that would accept the User. And then here, what would we do, let's create a new instance of a CommunityLink. And that way we can assign it. And then we could return the link. And by the way, here could be a good place to set whether it's approved or not. So you could do something like if this User is preApproved, or just isApproved, something like that. Maybe you have certain people on your site that you always get permission. Well, then there is where you could say linkApproved equals true. And that could be another little place to nest that logic. Anyways, lots of ways to do this. So there we set a User, and we return a new instance. And then we add a new method called contribute. And that will accept the attributes for the table row. And then there you might do something like
and we return a new instance. And then we add a new method called contribute. And that will accept the attributes for the table row. And then there you might do something like return $this->fill($attributes), and then save() it. And that's it. Okay, so does this all make sense? You submit the form, and we're going to say communityLink from the authenticated User. So we should add a middleware to ensure that in order to hit this method, you must be signed in. Anyways, we call a contribute method on that we pass through the $request data that will fill the model and persist it to the database. And then finally, we return back. Now, I think this is still going to fail on us. But if we give it another shot, yeah, well, we could import this or let's just use the helper function. So give me the authenticated User. Anyways, that is not the one I was expecting. Here we go. Okay,
Fix Form Names7:33
yeah, well, we could import this or let's just use the helper function. So give me the authenticated user. Anyways, that is not the one I was expecting. Here we go. Okay, so we took care of the user, but still the channel ID, we haven't set that up. And like I said, I want to do that separately in its own lesson. So for now, we're just going to throw it in here. But trust me, we're going to delete it probably in the next video. So I'm just going to hardcode one for now. Okay, one more shot. And now it's failing because it didn't have a title. So if we come back to our view, yeah, it looks like my snippet here, I'm not including the name. So none of that crap is getting sent through. We have to use the name attribute there. Okay, go to Google and contribute the link. And there we go. Okay, so that's really the first step. The second step is, well, what about the not happy path? What about if validation fails? In fact, we don't have
