Adding Channel Select0:00
Welcome back. We're now going to tackle channels. So if we go to our editor, let's go to our add link partial that we created in the last lesson. And right here at the top, let's add a section for choosing a channel. Okay, so we're going to have a label for channel, if we even need that. And this will be a select, right? This will be the channel_id. Leave off the ID for now. Okay, so for the options, I'm going to hard code two, and then we're going to swap that out with a more dynamic approach. So for example, we could say the default disabled option will just be one that says pick a channel. But then we'll have another one, for example, for, I don't know, PHP. And actually the value might be the ID of the channel in the database. So like two would be JavaScript, and you get the idea. So if I were to come back to Chrome and give this a refresh, yeah, there's our channel list. But let's go ahead
Validating Channel ID0:46
channel in the database. So like two would be JavaScript, and you get the idea. So if I were to come back to Chrome and give this a refresh, yeah, there's our channel list. But let's go ahead and style that, right? And I assume, let's see, I assume I have to give this a class of form-control. There it is. Much better. Oh, and also let's say channel here. Yeah, there we go. Much better. Okay, so now you choose a channel, you specify a title, you pick a link. So one thing we should do is go back to our Controller and make sure that channelId is required. And actually, let's do one more here. We'll make sure that it does exist within our channels database table. So that means we're going to look in the channels table, and specifically we're comparing against the ID. So what we're doing here is we're just making sure that they don't pass in, for example, this. Well, in that case, we would associate the community link with the channel with an ID of that,
the ID. So what we're doing here is we're just making sure that they don't pass in, for example, this. Well, in that case, we would associate the CommunityLink with the Channel with an ID of that, and we know that doesn't exist. So we're just going to force it to be some ID that exists within the channels table in the ID column. Next, now that we've done that, we can go back to that fromStatic constructor. And remember, when we temporarily added this, I can remove that entirely. And then on that note, we could even clean this up a bit by saying return new static, and set the userID equal to the user passed in. Yeah, now we can do that. So let me get my relationship, bring that to the bottom, and starting to get there. Okay, so right now, if we try it, I think it should fail. So let's pick something, gibberish, and then some kind of link. Contribute. Yeah, it redirects back because validation did fail, but we're not yet displaying
Creating Channels in Database3:16
And real quick, let me do a bit of cleanup. Now, one thing I often like to do is put each of these, when you have an input or any element with tons of attributes, I generally like to put them on their own lane like this. But yeah, usually, I don't do it for screencasts just so we can go a little bit quicker. Okay, so anyways, let's bring back php artisan tinker and whip up some. So we could say, let's use app Channel. And for a quick recap, if we go to our channels table migration, it consists of the title, the slug, and a color, if you want to do that. Like I said, there's ways around that. That one just depends. So anyways, let's say Channel::create, where the title is PHP, the slug will be php, and then the color will be red. Okay, so we run that, but we get a mass assignment exception. And that's because right now, our channel table has no fillable fields, so it's protecting us. Let's do this. title, php artisan tinker,
but we get a MassAssignmentException. And that's because right now, our channel table has no fillable fields, so it's protecting us. Let's do this. title, php artisan tinker, title, slug, color. Okay, one more time. And that works. Let's do another one. JavaScript will be green, and the title will be JavaScript. And then finally, one last one, pink, and the slug will be, I don't know, Ruby, and the title will be Ruby. Okay, so now at this point, we have three different channels in the database. So now, we have these IDs that we can associate with any community link. So let's do this. Back to our add link page, and rather than hard coding this, we'll do it dynamically. Maybe something like this. We'll say for each channels, maybe we'll grab some channels in a minute. So for each one, we will spit out an option. And we'll set the value equal to the ID
Rendering Channels Dynamically5:04
Maybe something like this. We'll say for each channels, maybe we'll grab some channels in a minute. So for each one, we will spit out an option. And we'll set the value equal to the ID of the channel. And then the text itself will be channel title. Now, if we fetch the channels, we'll go back to our controller here, and we can grab it at the top. channels would be channelAll, and pass that through. If we come back, and we give it a refresh, let's see. Ah, we forgot to import that. All right, one more time. So if we select, we should see three options, and we do. But now notice they're not in alphabetical order. They're ordered by the ID. Well, if we want them in alphabetical order, we could just say orderBy the title, and ascending, and give me the results. So now if we come back, yeah, this stuff is so easy. It's in alphabetical order. But now we do have one problem. Imagine that you pick a channel,
Preserving Old Form Values5:56
and ascending, and give me the results. So now if we come back, yeah, this stuff is so easy. It's in alphabetical order. But now we do have one problem. Imagine that you pick a channel, but you forget something else. So maybe you set a title, but you don't use a valid link. Well, this is going to fail validation. And we want it to redirect, but populate all of the fields from before. But if we do it right now, we don't see anything. Okay, so we can do this pretty easily. If we come back, you could do this number here. You could set the value equal to the old title. And that's very simple. So if we try it again, contributeLink, yeah, it remembers the value. So we're going to do that for each of these here. value is the old link in this case. But for the select, yeah, you can't do value here. You can't do anything like this, oldChannelID. So here's what we have to do. We're going to do
Value is the old link in this case. But for the select, yeah, you can't do value here. You can't do anything like this, oldChannelId. So here's what we have to do. We're going to do it on the option itself. And we'll say, if the, let's do this, if the oldChannelId equals the currentChannelId in our loop, well, in that case, it's the selected one. So we could do selected, otherwise nothing. And then we'll put this on its own line. Okay, so I think that should do it. Let's double check. Choose Ruby. It should redirect back. And now this time it remembers the option that you selected. All right, so does that part make sense? We're looping through all the channels. And for each one, we're creating an option. But we also have this check where we say, well, is the old value from the previous form submission, is that ID equal to this current one in the list? If so, that's the one they last selected. So let's go ahead and add the selected
Auth and Uniqueness Fixes7:37
well, is the old value from the previous form submission, is that ID equal to this current one in the list? If so, that's the one they last selected. So let's go ahead and add the selected attribute to automatically override this and make it the default. Okay, so I think we're doing pretty good at this point. Why don't we try it? php, some PHP article. And we'll just have it go to Google. If we contribute it, we have a mistake here. So argument1 passed to communityLinkFrom must be an instance of User, but null was given. Okay, so this is something we haven't done yet. We talked about adding a middleware to basically prevent you from contributing a link if you're not registered or signed in, and we're not. So really, I shouldn't even see this at all. Let's fix that. Right up here at the top, we could say, if auth()->check(). There we go. Please sign in. You can make that a link. You can do whatever you want. So let's log in with John.
Let's fix that. Right up here at the top, we could say, if auth check. There we go. Please sign in. You can make that a link. You can do whatever you want. So let's log in with John. All right, we're going to go to that community page. And now one more time, a php article that goes to Google. So what do we have this time? Integrity constraint violation with User. Yeah. Okay, so I already know this one. This is because if I go back to community link, yeah, this section right here. So user_id is not mass assignable. It's not included in that list. And you know what, it turns out that's why we did it this way before. So for example, we could say, $link = new StaticLink(); $user_id = $this; Yeah, this is why we did it this way before. Fair enough. You could do this. There's also some things you could do. Like, if you're creating a model, there is a forceCreate. So basically, this allows you to do it.
Fair enough. You could do this. There's also some things you could do. Like, if you're creating a model, there is a forceCreate. So basically, this allows you to do it regardless of whether it's mass assignable or not. And I think there's also a forceDelete and also forceFill. So that's good to know. Only do that when you're 100% sure there's no problem. And generally, that's when you are in complete control of all of the attribute values. But yeah, this is fine in this case. All right, let's give it another try. Ah, okay, now we're on to another one. But this is good. This is something we forgot in our validation. So we already have, I'll show you. If we do php artisan tinker, app, communityLink, all, we already have one that goes to Google. And remember, when we created the migration, we decided that this column should be unique. We don't want multiple links being posted that
we already have one that goes to Google. And remember, when we created the migration, we decided that this column should be unique. We don't want multiple links being posted that go to the exact same endpoint. So this is failing because we didn't catch it in our validation. So we can do that by going back to the controller. And then right here, yeah, we could say this. So we will add unique on the community_links table. Okay, let's see if that one works now. So if we, let's go back. We're going to try to submit this link, but it already exists in the table. So it should now fail validation. And it says the link has already been taken. Now, you'll find that with Laracasts, I do this slightly differently. I don't add it to the validation stuff. I do it later, where I check the database, and then I touch the timestamps. But maybe we'll get to that later. This is fine for now. So we'll say,
see it there. And then you can style it however you want to. For the background color, yeah, once again, you could do a couple options. If you don't want to store the color in the database, which is fair enough, you could do link, and then get the channel associated with it. Once again, no eager loading just yet. We'll do that soon. And maybe you could add the select there. That would be an option. And then in your CSS, you would just say PHP should have a background color of red, and JavaScript should have a background color of green. Or you could even tack on channel-PHP. You get the idea. You could do that. Or if you want to keep it in the database, yeah, you could do something like just embed a style tag here. Background color would be link channel color. And like I said, that would I would only reach for that if maybe I'm building a CMS where I want people to very easily choose the color they
tag here. Background color would be link channel color. And like I said, that would I would only reach for that if maybe I'm building a CMS where I want people to very easily choose the color they want and not worry about CSS or anything like that. So right now, this should fail, right? Because we don't have a relationship between a link and a channel. So we go to that right down here. Channel and what is the relationship here? A community link is assigned a channel. So once again, we have a belongsTo relationship. Okay, will that do the trick? Give it a refresh. And there you go. So all of these were assigned to PHP, I think. So let's add one to JavaScript, some JS article. And this time, it'll go to Google. All right, contribute the link. And it works. So in this case, it still says PHP, because we're still hard coding it. But that's an easy one at this point.
