Series Goals Overview0:00
Our goal for this series is to dig into Eloquent, but at a more advanced level. So I'm going to assume that you already understand the basics. You understand the syntax. You understand basic relationships and things like that. But if you now want to take another step and really peek into, for example, what's going on behind the scenes and how the source code is set up for Eloquent, well, in that case, this series is just for you. All right, so for this very first video, before we dig into Eloquent, I want to set up the world, so to speak. So Laravel, new, Eloquent series.
Install Dev Packages0:30
I want to set up the world, so to speak. So Laravel, new, Eloquent series. And now if we cd in, I want to pull in a couple packages, mostly just to make our workflow a little easier. As you can imagine, we will be setting up tables and models and migrations. We will need to seed these tables. So I just want to make that workflow as simple as possible for me. Okay, so composer require. We're going to get laracasts/generators. It gives us an easy way to not only generate a migration,
We're going to get Laracasts slash generators. It gives us an easy way to not only generate a migration, but also the schema in the process. So it's pretty useful. All right, that's done. And really quickly, it looks like I need to update Composer itself. And now I just want to grab one more. We're going to grab Laracasts test dummy, which is a tool I built mostly for testing. It's great for integration testing where you quickly want to whip up any number of rows for the purposes of testing some query.
It's great for integration testing where you quickly want to whip up any number of rows for the purposes of testing some query. But we are also going to use this for easy database seeding. So I will pull that in. But also for both of these, actually, they're only necessary for development. And there we go. That one is pulled in too. Okay, so I'm going to go to my composer.json file and fix my little mistake there, where that should be for development. Next, we will go to config/app.php to register the service provider for the generators.
Register Service Provider1:43
and fix my little mistake there, where that should be for development. Next, we will go to config/app.php to register the service provider for the generators. Now, technically, because this is only for development, we should put it within like an AppServiceProvider. But we're not actually building anything. So I'm not too concerned about putting it here. Laracasts\Generators\GeneratorsServiceProvider. Cool. So now if we run php artisan, I just have an alias for that. And if we scroll up, you'll see that we have a number of new commands that we can run,
Create Posts Migration2:10
So now if we run php artisan, I just have an alias for that. And if we scroll up, you'll see that we have a number of new commands that we can run, including one to very quickly create a table seeder. So now to finish out this lesson, and at least get us started so that we can run some queries, why don't we have something like a Post model, and then maybe we will insert 50 rows into that table. Let's do that pretty quick. All right, php artisan make:migration with schema. I'm going to call it create_posts_table. And the schema will be well, a Post belongs to a User.
I'm going to call it create_post_table. And the schema will be well, a Post belongs to a User. So let's say user_id is integer. And it's also going to be a foreign key. Next, I want a title and then a body. And why don't we leave it at that? Okay, so if you're not using these packages, but you are working along, let me show you what that looks like. I'm going to go into database/migrations. And you'll see that the benefit is it built up this schema for us automatically.
Configure SQLite Database3:03
I'm going to go into database migrations. And you'll see that the benefit is it built up this schema for us automatically. So it really does save you a lot of time. And then also, of course, because this is a fresh install of Laravel, it includes a migration to create a users table, as well as password resets. Now, in this case, that's not even necessary. So I've deleted that. Next, I'm going to go into my database configuration file. And I'm just going to use sqlite here. And if we scroll down, we'll see that by default,
And I'm just going to use sqlite here. And if we scroll down, we'll see that by default, it's going to look for a database.sqlite file. So we will touch storage and then that file. Okay, so now our database is set up, which means I can migrate it to build the users table, as well as the post table. Cool. Now next, you'll see if I open up the app directory, that we already have the Post model. And that's because once again, we used that command.
Seed Data with Factories3:55
that we already have the Post model. And that's because once again, we used that command. It knew that we were creating a new migration. So we probably also want a Post model. It's just a convenience. Okay, so we have our migration, we have the database set up, we have the table. Now I just want to populate it with some dummy data. And that's what we use table seeders for. php artisan make:seeder for the Post table. All right, so now if we open up our seeds folder,
php artisan make:seed for the Post table. All right, so now if we open up our seeds folder, you'll see that we have this new class here. And it includes just a little hint of what you might want to use. So we've already pulled in TestDummy, which means we can get started using this right away. Here, if I uncomment this, I'm going to bump it up to 50. 50 times I want to create a Post. Now if we go to our parent class, we need to make sure that we trigger any of these various table seeders that we've created.
Now if we go to our parent class, we need to make sure that we trigger any of these various table seeders that we've created. So I can uncomment that and say, PostTableSeeder. All right, so this is looking pretty good. But right now it's going to fail. Let's see. php artisan db:seed. And yeah, it fails. And that's because we haven't really told TestDummy what the makeup of the post table looks like.
And that's because we haven't really told testDummy what the makeup of the post table looks like. We just have to do that once. And we can see we can put that in a tests/factories directory. Or really, you can override that and put it anywhere you want. But that's fine as a default. That way we can use it when writing our integration tests at the same time. So right here, I will have a factories directory. And within it, why don't we call this All.php. And this is where we will define the makeup for all of our tables.
And within it, why don't we call this all.php. And this is where we will define the makeup for all of our tables. Factory for the Post class. So we have a title. Now within this file or any files within this folder, you'll have access to a faker object. And this gives you an easy way to call simple properties that will return dummy data. So if you need an address or a name or an email address or anything like that, faker is really useful for that. So the title of our post will be a dummy sentence. The body will be a dummy paragraph.
So the title of our post will be a dummy sentence. The body will be a dummy paragraph. So we can call paragraph. And then finally, we have the userId as well. And that will be, well, we don't want to just do something like one. This needs to be dynamic. So what I really want to say is this Post that you create needs to be associated and belong to a User. We use a special syntax for that with test dummy. Just write factory and then the name of the class App\User. So now when it sees this, it'll go,
Just write factory and then the name of the class User. So now when it sees this, it'll go, OK, it looks like the user_id actually references another relationship. So I need to whip that one up as well and then assign that new ID in its place. All right, we're just about ready to go. But now we have another one. So we need to define the makeup of a User. Let's take a look at the migration. Create users table. And we have a name, email and password.
Create users table. And we have a name, email and password. And that should do the trick. Name will be faker()->name. Email will be faker()->email. And password. I'm not sure if there's a password attribute off the top of my head, but I do know there's word and that's really all we need. And don't forget, if that needs to be encrypted, then you could wrap that within bcrypt. OK, so that alone should do the trick for us.
And don't forget, if that needs to be encrypted, then you could wrap that within bcrypt. OK, so that alone should do the trick for us. And the cool thing is you only have to do this once for the lifetime of your project. Now, whether you are seeding a table or writing integration tests, you're good to go. So let's try this out. php artisan db:seed. And if we run this, we should now have 50 rows within that posts table. Let's see. SELECT * FROM posts. And if I zoom out, there you go.
Select * from posts. And if I zoom out, there you go. Tons of dummy data to get us started in this series. And by the way, notice that the user ID was, in fact, set dynamically. So if I select * from users, there's that dummy user that we whipped up. So that means we've set up the migrations, we've set up the Eloquent models, and we've set up the table seeding. So with all of that prep out of the way, in the next video, let's start digging in to Eloquent.
