Starting New Laravel App0:32
So nothing too fancy, but you know what? I think there's a lot to learn here. So let's get started. From scratch, Laravel, new, and we'll call this voting lesson. All right, let's go in there and open this up in my editor. Okay, so let's get started. I'm thinking, well, let's go back to Chrome real quick. I'm thinking we should get started with a table. So what does this represent? We're going to call it community links.
Creating Community Links Migration0:57
So what does this represent? We're going to call it community links. Fair enough, right? So php artisan make:migration called create_community_links_table. And the table we are creating is called community_links. Okay, now if we switch back, we'll go to that table. And what is the makeup for this? Well, well, let's just go real quick and take a look. One, you need to know who created it. So we need the user_id.
One, you need to know who created it. So we need the userId. We would need the channelId or the categoryId. We need the title. We need some timestamps for when it's created. We need the actual link. So when I click on this, where is it going to direct us? And then maybe one other thing I would want for now is a simple boolean. So something like an approved column. So by default, when you submit something, it's not approved and
So something like an approved column. So by default, when you submit something, it's not approved and it doesn't display here. However, once I do approve it, it does show up, all right? So I think that should be enough to get started. We need the userId, and we can add an index to that. Next, like I said, we need a channelId. Now, we don't even have a channels table set up yet, but I'm anticipating that. Next, let's see, we need a string for the title, next a string for the link. And actually on this note, one thing we might want to do is, well,
Next, let's see, we need a string for the title, next a string for the link. And actually on this note, one thing we might want to do is, well, we want to make sure that people don't post the exact same link over and over. So for example, if this guy promotes LayerFell 4 John Azur, well, tomorrow I don't want somebody else posting the exact same thing. So we can do a little bit of checking there. One, we could make this a unique column. So you can't insert a new row that goes to the exact same URL. That's already represented, right? So we'll do that.
Adding Channels Table2:42
That's already represented, right? So we'll do that. And then finally, we have a boolean for whether it's approved or not. And we'll say the default is going to be zero. By default, no links are approved. Okay, that looks good to me. So next, why don't we go ahead and create this channels table? php artisan make:migration create_channels, or you might call it categories, or anything you want. I just call it channels at LayerCast.
you might call it categories, or anything you want. I just call it channels at LayerCast. All right, create channels table. And now this one should have a title. So this would be like a readable version. So you can imagine like do it yourself, maybe something like that. That would be the title, but a slug, we'll do another one here for the slug. That version might be do it yourself. So I don't know, maybe we can add a separate column for the slug. We'll see if we even need something like that.
Yeah, so lots of red here, but you can see every different channel has its own color. And you can do this in a lot of ways. One, you could just add a class name and make that equal to the slug. And then you can just style that within your CSS. That would definitely be an option. Another option, especially if you're building a CMS for somebody else, is maybe they just want to assign a color. So in those situations, you could store it in a table. Just be careful with stuff like that, okay? Finally, timestamps, and yeah, I think this is fine.
Configuring SQLite and Migrating4:23
Just be careful with stuff like that, okay? Finally, timestamps, and yeah, I think this is fine. All right, so well, why don't we go ahead and migrate the database? But it's not gonna work, right? And that's because it's a brand new install. So why don't we set up our database connections? I often use SQLite so we can get rid of all of the database information there. Next, I'm going to touch database/database.sqlite to create that. Okay, now we can migrate my database and we have the users table, which comes by default, and then a community_links and a channels table.
Scaffolding Authentication4:50
Okay, now we can migrate my database and we have the users table, which comes by default, and then a community_links and a channels table. Okay, next, if I run php artisan, I do know that we need users in our system. So why don't we leverage Laravel to do this? I'm gonna run php artisan make:auth to scaffold some stuff for us. Okay, so that gives us a bunch of views, a HomeController, and it updates the routes file. If we take a look at that now, yeah, we have Route::auth(). In fact, let me take you to that real quick. This will just give us a handful of links that you see right here.
In fact, let me take you to that real quick. This will just give us a handful of links that you see right here. Okay, so at this point, I am using Laravel Valet, and this is in my code directory, so I should be able to go to that URL, voting-lesson.dev. And there we go, pretty cool. And by the way, if you're not familiar with Laravel Valet, of course, we cover it here at Laracasts. That will show you how to get up and running. Okay, anyways, so we have a new application. We can register, and in fact, let's just do John Doe with pass and pass.
Okay, anyways, so we have a new application. We can register, and in fact, let's just do John Doe with pass and pass. Whoops, I don't even know the requirements here. One more time. Okay, so now John Doe has an account, and he is signed in. We can log him out, all of that good stuff. So next, why don't we, let's see where we are at this point. We've created two tables, right? We've created a channels table, and we've created a community_links table. Now, I didn't create the model in the process, so
Generating Eloquent Models6:22
We've created a channels table, and we've created a community_links table. Now, I didn't create the model in the process, so why don't we go ahead and do that now? Make, by the way, if you ever see me do artisan, that's just an alias for php artisan, nothing but a shortcut. Anyways, let's make a model for the Channel, so that represents a category in our system. And then I'm gonna do another one for, yeah, maybe I'll just call this CommunityLink for now. Now, a quick little tip.
maybe I'll just call this CommunityLink for now. Now, a quick little tip. If you want, you could say php artisan make:model CommunityLink. That will also create a migration file in the process. I often forget to do this, but really, this is a good way to go if you're starting from scratch. Otherwise, you have to do what I did, where you run the full php artisan make:migration command, and then you run php artisan make:model, so it's extra work. Okay, but anyways, we have our channel, and we have a CommunityLink. So I think we're gonna stop there for this lesson.
