Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:12Length:2.1 hours
  • Overview
  • Transcript

2.2 Seeding

An empty database is a useless database, especially when developing an application. In this lesson, I'll show you how to seed your database with test data so that we have something to work with.

2.2 Seeding

In the previous lesson, we looked at migrations. And we used a migration to define our tables schema, and then we ran that migration to actually create that table for us in the database, so that we didn't have to do so, writing any incantation through sequel to create that table, it was just done for us by eloquent. And if you'll remember there were two other migrations as well. We didn't write them, but they were given to us by the Laravel Project itself. So the password resets and the users table were given to us by our Laravel Project. Our guitars table is our own doing. So that is what we are going to focus on, and of course we don't have any data here, and that is an issue because as you write data driven applications. Which, let's face it, just about every application you will write. We'll be working with data in some way in order to actually develop your application, you need data, so that you can see that your application is doing what it needs to do. So we use a process called seeding, it's not unique to Laravel, or PHP, it's just the idea of seeding a database with data and in our case, it doesn't matter what that data is as long as we have data to work with. So you really wouldn't use seeding for the end user. So if somebody was going to take your application and run it on their server. You typically, wouldn't allow them to do any type of seeding. It's primarily for our own purposes. And the way that seeding works in Laravel is you create a class, and then you use that class to seed whatever table that you want to seed. So whenever you create a seeding class, it's going to go inside of the database folder. Inside of the seeds folder and it's going to end up here. Now, by default, we have this database seeder and we could technically use that, but it's typically a good idea to create a seeder for each individual table that you have. So let's go to the command line and we are going to create a seeder for our guitars table. So we say php artisan make and we want to make a seeder, and then we specify the name. So we are going to be populating the guitars table, so I'm going to be creative and say, Guitars Tables Seeder. I'm sure that there's a better name, but one of the things about programming is it's hard to come up with names. So we're just going to stick with this. It is at least descriptive as to what it is. Now, we have this GuitarsTableSeeder and that's not what I want. I want Table to be singular, so lets go back, let's run this. And these classes are very simple. There's just a single method called Run, and inside of this method is the code that we are going to have for seeding our database. Now, you technically don't have to do anything inside of this run method. If you wanted to you could say echo hello guitars table seeder class. And whenever we actually ran this seeding process, then we are going to see that here in the console. So if we say php artisan db:seed, and then we specify the class, which is GuitarsTableSeeder, then we're going to see hello guitar seeder class. So you don't actually to have any database code here, but that kind of defeats the purpose of having a seeder class to begin with. So here's what we want to do that we want to populate our guitars table with data and we do so with the db facade. It has a table static method, and then we simply specify the table that we want to seed. So we say guitars and this has a method called inserts. We are inserting a row into the guitar's table, and then we pass in an array that has the columns and their values. Now, as far as our guitars table right now, we just have a name column. So our key is going to be name, and then the value is going to be whatever name the guitar is going to be. If we had other columns that we're going to populate, like manufacturer, then we would do the same thing. We would have a key called manufacturer, and then the value would be whoever made that guitar. We'll get there, but for now we're just going to work with our name column, so let's copy this a few times and paste it. We have a Vela, we have a Starla, we have a Les Paul. Let's also have an Explorer, and we definitely need a Strat here. So now, let's go back to the command line, let's run that seed command. And then we will hop on over to the database. And let's look at this again, we'll refresh. And see what we get, hopefully, we'll have our data and we do. Now, notice the ID is automatically set for us, that is off course, the nature of the auto increment field, but we have the our Vala, we have our Strala, Las Paul, Explorer, Strat, but the createdAt and updatedAt are set to null. That's okay, I mean, if we really wanted some data there, we could populate that. And all we would have to do is just simply add another key value pair and say, created_at, and then the value. But once again we're just going to stick with the names. Now, we've hard coded these values, and that's okay in this case, because I do want some actual guitars, so that I can actually see that through some data here that makes sense, but a lot of times we need a lot of data. And while we could manually insert that data, a lot of times it doesn't really make sense to do so. And just random data will be sufficient. So we can do that with a four loop or really any type of loop, but in this case, we need a counter, because we want to add an arbitrary amount of guitars. And I'm just going to make 20. So we're going to iterate for 20 times, and we are essentially, going to do the same thing. We're going to use the DB class, the table method, guitars, and then insert, but instead of an actual value, we are going to generate a random string. And, let's just make this 12 characters long, and that will be fine. So let's run our seed command once again. Let's go and look at the database. So we have these five guitars here. So whenever we refresh this, we should have the 5 guitars and then 20 random guitars. So let's just reload the results and what do we have? Well, we have five guitars, yes, but then we have the same 5 guitars again, followed by our random guitars. So here's the thing, whenever you call the seed command, it is just going to take what you have written inside of the run method. It's not going to take into account Anything that exists in that table is just going to insert more rows or whatever you have specified inside of this method. So that's fine in some cases, but in most cases, whenever you see the database, you want to start completely over from scratch. So that whenever you have written a lot of your application, you've really just messed up your data and you want to start completely from scratch. Well, that's what the seed method is for. It is to seed your database with starting information. So in our case, what we want to do is called truncating. We want to truncate the guitars table, so that it wipes everything out of the table. That's also going to reseed the ID. If we don't reseed the ID, then the next row that we insert is going to have an ID of 31. And in the grand scheme of things that really doesn't matter, but it does to me. I'd like my auto-incrementing column to begin at one. So all we have to do is call a method. Once again, we still have to specify guitars and all of that stuff. But instead of calling insert, we're going to call a method called truncate. And that is going to wipe out the table, it's also going to recede our ID column. So let's run the C command again, and whenever we look at this in the database, we are going to see our list of 5 guitars, and then our 20 random guitars. Well, before we go let's do this, let's create another table. So lets say php artisan, we want to make a migration and we want a manufacturers table, so we'll call this create manufacturers table, and then whenever this creates a migration which has, let's go to our code, and inside of the migrations, there is the create manufacturers. Now, in this case, all we're going to do is have a column, that's going to be a string called a name. In a real application, we would have an address and things like that. But the name is going to be sufficient in this particular case. And let's go ahead and migrate, so that we have that table. So php artisan migrate, and we will have that manufacturers table. So we now have two tables that we need to seed, we have the guitars and we have our manufacturers. So let's go ahead and create a seeder for our new table. So artisan make seeder and we'll call this ManufacturersTableSeeder. Manufacturers is difficult to say, it's also difficult to type as well. But this will give us our seeder, and really we can take what we have inside of our guitars seeder, and use that as a basis and we'll just do that, we'll copy and paste we want our truncate. We do need to change the table here. This is a new table, so we are saying manufacturers and lets do this. We'll have three manufacturers, we are going to hardcode this, because I do not want random manufacturers. Because, well, there's quite a few manufacturers, but we're just gonna deal with three. We're going to use Paul Reed Smith, going to use Fender, and then Gibson, so that we have those. So we have two seeder files that we can use to seed our database, so we could come to the command line and we could actually run those files just as we did with out guitars table seeder, but as your probably thinking right now, I don't want to do that, that's going to take a lot of time especially as our application grows. And we get more and more and more tables. So what we can do is use this database seeder class that was already here to begin with and you can see that there is this commented out code. It's using a method called call and it is specifying this UsersTableSeeder class. So we can do the same thing. We can specify all of our seeders, so that we just have to run one command and it's going to use all of our seeders to seed our database. So we're going to use the call method. And instead passing in a single seeder class, what we're going to do is passing an array of our seeder classes. So we will have guitars, table seeder, and we have specified class and the same thing for ManufacturesTableSeeder and class. So now, at the command line we don't have to specify a file or anything like that. We just say php artisan db seed and it's going to use that database seeder class. It's going to call its run method and we can see here that it is seeding the guitars table seeder and it seeded the manufacturers table. So let's go to our database. Let's look at our tables. We have the guitars table which we can look at and we have our 5 guitars. Followed by the 20 random guitars. Let's also look at the manufacturers, if we go there, we see our three manufacturers. So now, that we have defined our database schema, we have data to work with, now we want to be able to work with that data within our application. And we could do that now, but typically, we use what are called model classes, and we are going to look at that in the next lesson.

Back to the top