From the course: Advanced Laravel

Seeding and factories - Laravel Tutorial

From the course: Advanced Laravel

Seeding and factories

- [Instructor] Now it's time to go ahead and implement the member features of being able to view all the scheduled classes and book slots for the same. For that, we need a good amount of data to work with. We need a lot of scheduled classes from multiple users, multiple instructors, and we also later need a lot of members to test other functionalities. You already know how tedious it is to manually sign up instructors and scheduled classes. The good news is we can use seeders and factories to populate dummy data for building our app. Let's seed our user database first with some instructors and members. Let's create a seeder, using sail artisan make:seeder User. Open this. Within this we have a run method. In here we can insert data as we want. If we want to insert users one by one, you can use the query builder like so, DB::table users and manually insert each and every user. But this is again, a lot of work when we need multiple records. So we can use model factories in combination with seeders. A user factory is already created for us in database, factories. Here it is, UserFactory. Look at the definition here. This definition method is used to specify how to create the dummy data. The other values is the PHP library fake for the same. Fake can generate different types of random data for you like names, unique emails, and you know, even words, addresses and so on. Once a factory is defined this way, we can use it in our seeders, like so. User::factory, count, then create. So when you say count, then create, you will get 10 users using the definition, the factory definition. Let me import the user model before I forget. Alright, this creates 10 random users based on the definition. But in our case, additionally, we need to specify a role for each user. So let's define a default role as member right here. And then this will create 10 members for us. What if we need 10 instructors? We can just override the role value here as instructor. Now this will give us 10 instructors. Note that every time we refresh the database for some reason these 10 users are created and every time the users are different. But for us to build features while testing in the browser, we need some fixed user email and password to log in, right? For that reason, let me create some fixed user data as well using User::factory, count. Let me not specify count because if I don't specify, it'll create only one. Create. Now, I'll say I want a fixed user with name Shruti and email shurti@example.com. So I'll be able to log in with this member every time I need to check. Let me also create another member. Let's say Taylor. Name is Taylor. I'll create a fixed instructor with name John and john@example.com. Here I also need to specify the role as instructor. And lastly an admin, to log in as admin. And sure the role will be admin. Okay, now let's reset our database using sail artisan migrate:refresh. Let's see what this does. Oh, sorry, I have a typo, yes. Migrate:refresh. And now if you look at TablePlus, all our data is gone, all the user's data is gone. So let's seed it using our seeder. Right here. Clear. Sail artisan db:seed and specify the class that you wish to use. UserSeeder. Seeding is done. Now let me go back and refresh the table. This is wonderful. We have 24 random users, actually four fixed users and the remaining 20 are randomly created using our seeders and factory. Now this was just one seeder, UserSeeder. We'll obviously have multiple seeders to see the schedule classes table, the class types and so on. And we cannot individually call each seeder this way. So what we can do is we can go to the DatabaseSeeder, which is right here created for us. And in the run method we can call every single seeder that we want to create at one shot, right? So here I'll say this call and pass in the array of all the seeder. So first we have UserSeeder and then let's create some more seeders next. But before that, let me show you how this works. Let's go back here. And instead of db:seed, let's say sail artisan migrate:refresh, so we are resetting the database and seeding it at the same time. We'll be using this quite a lot during our development. So now you can see it's rolling back all the migrations, running the migrations and seeding the database. If you go back and refresh, you have new set of data but you still have 24 users. All right, let's create seeds for class types table and scheduled classes table too. Clear. Sail artisan make:seeder ClassTypeSeeder. And sail artisan make:seeder ScheduledClassSeeder as well. All right. Oh, there is a small typo. I'm sorry, ClassTypeSeeder. Okay, I'll go back and delete this. All right, open this. Let me delete the typo one. Delete. Okay, so in our ClassTypeSeeder, we don't want any random data. It's better if we have fixed data like we created like yoga, dance, fitness, so that it makes sense. That is why I will not use a factory for this. I will seed the data manually here, like ClassType::create using aloquin. Let me create four different classes. So instead of doing it in TablePlus, we are doing it here. But the thing is we can do it just once. And description, you can use fake for the same, fake text. So that will give us some fake text. And minutes, let's say 60. Okay, so this will create one class. Let me copy past it four more times and change some things. This is 45 minutes, let's say pilates, 16 minutes and boxing for 15 minutes. Okay, this is done. For our ScheduledClassSeeder. Where is that? It's here. We will need a factory because we need too many classes. So let's create a factory for this. Sail artisan make:factory ScheduledClassFactory. Okay, let's add our definition here. Return instructor ID is one thing we need. And this instructor ID can be a random number within 15 to 24. That's because we are anyway seeding the database the same way, right? Let me open up UserSeeder. Yeah, since we are first seeding four different fixed users and then we are seeding 10 members, that's why the instructors will have IDs anywhere between 15 and 24. 'Cause this is not the best way, but for now, let me go with this. It's a random number between 15 and 24. Then the class_type_id is a random number between one and four because we have only four classes. And then comes the date_time, which has to be a random one again. Let me use Carbon::now. And it has to be any future date and time, so let me add some hours to this. AddHours. Anywhere between 24 and 120 so that, you know, hopefully we will not get, duplicate date time. And here I'll say minutes, zero and seconds, also zero so that it starts at the zero minute and zero second. So we get any random hour. Obviously we do get values like 3:00 AM and 12:00 PM when we might not have gym classes but it's okay to work with. So this gives us a random hour within the next five days. All right, now let's use this factory to schedule 10 classes in our ScheduledClassSeeder. Let's say ScheduledClass::factory count, 10, create. We have both our seeders. Let's not forget to include them here in the DatabaseSeeder. UserClass, ClassType, sorry, ClassTypeSeeder::class. And then our ScheduledClassSeeder. You have to include ScheduledClassSeeder after these two because note that we have foreign keys referencing both these tables here. So this should work. Let's try once sail artisan migrate:refresh --seed. Let's see if all three tables are seeded. Rolling back migrations, running migrations. All right, we do have an error. It says unknown column type. I'm so sorry, it was not type. It is supposed to be name. Okay, let's try once again. Okay, rolling back, running. Carbon is not found. Again, I have to include it. Where is that? It's right here. Yes. Hopefully this time it'll work. Okay, there's a duplicate entry but I think this occurs very rarely. It just had to occur now. No problem. Let's try one last time. I think there's one more mistake. I cannot add hours like this. I'll have to have a random function within this. Sorry, yeah. And now this should work. Clear. Rolling back migrations. Running migrations. Great, all three seeders have run successfully. Let me check in TablePlus. Close users, scheduled_classes, refresh, and we have 10 classes scheduled. We have four class types as well. Perfect. Now we can continue building all our features.

Contents