Seeding the Database Part 2
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
9 hours 41 minutes
I welcome back to module for this is less than 4.9 seeding the database to I had hoped Teoh keep this down to two videos, but I think it's going to go into three videos. So in this lesson, we're going to continue building the factories. The user factory has already been built. I'll quickly review it,
and it will be available for download as a resource as well.
We're also going to in this lesson, create the grade factory. We're going to create the user type factory. There were a test and validate that. The factories we have so far are indeed working. And what that said, Let's get started.
I welcome back. So let's go over the changes that have already implemented
and the users table creation migration. I've added a couple fields. I've added the password field
and a Boolean activated field, and that is just because you will typically find these fields on user tables in data basis
and in the database cedar for the user factory
I've created, just like the previous two factor to factories that we created in the previous lesson.
And I've added faker calls to populate that with data
and in the cedar. I'm truncating the user
table and I'm running a factory on the user factory.
I'm not using the concurrency, and that is because the user table actually doesn't have any dependencies on other tables.
So we have tables that depend on the user to exist. But we don't have any fields on the user's table that
that's depend on existence in some alternate table, so we don't need to use that concurrency function in the cedar class.
And one more note. Remember to include your use app
backslash user at the top of your file with the semicolon at the end. If you forget that and you go to seed and you make a call to the user class, the program is not going to know what a user is, and it will crash on you Now. That said, let's start making the next factory
so we have the users. Let's go ahead and make the grave factory. We need to make sure we have a shell session open into our vagrant machine.
Make sure we are in the right directory, so I need a CD. It's level
ls, and I'm gonna go ahead and do a switch or a suitable dash s to switch to the suitable user just to make sure I don't run it any permission errors,
and then I'm going to hit up.
And I have my last factory call, which was the user factory.
Go ahead and change that to the grade factory.
There we go. Now, if you if you've added those fields to the user table as I did and you've
change your DB cedar to include the create statement,
you will need to migrate a fresh database before you attempt to seed. Because you're old database won't have the new fields. And when you go to seed with the user factory, you'll get in air because the password won't exist and the activated won't exist in the database. So keep that in mind if you just
jumped ahead to seed
fake data with the user data.
So let's take a look at what we have in our grade table. I'm gonna open up D beaver.
I wanna go to my grades.
Men go to the Properties tab.
I see that I have a user idea. Course I d letter grade grade score. OK,
so we need to get that factory open, and that's gonna be in the database factories
grade factory. I'm gonna edit that with no pad plus plus
and here we go. So first we need a user, I d.
So we need to rant role a random user.
Let's open up another factory where we're doing that, and we'll just change it to the user class.
Let's see, I think the room factory has one in it.
Yes, we're getting the building.
So I'm gonna go back to the grave factor will put this at the top, and we're gonna change the wording to make sense and line up with the model we're using
and the rest will work. And, of course, we need the use
user at top. Otherwise, that won't work.
So then we'll drop down. We'll start our array of return. So it's a user
underscore i d
Next is the course I d. So I'm just gonna copy this
and then I'm going again change the wording. So coarse
and again, we're going to need that use statement at the top to make sure that this file understands what of course is
try align those up there. So course underscore. I d
That's in tax, right? We need those dollar signs. We need those commas. We need those quotes.
And next we have a letter grade. Well, this is does not depend on other table dad data. So we're going to roll a random element.
Let her grade.
Yeah, Let her grave. Big arrow faker
random element. This is a function in faker.
We're gonna create an array. That's what the brackets on. We're gonna go a
And then we're going to do our course grade
a great score,
And I've copied this from the faker documentation.
We're gonna get a random float. The Max Death Smalls
is gonna be too.
Our minimum will be zero and our max will be
Okay, so we haven't lined this up. We could see some things that might look odd if we know anything about grading. So we might see an f with a 98 or in a with a 30 for testing. That's fine. That's Ah, It would be a lot of trouble to We'd have to implement functions that say
Okay, he rolled in A.
That means he needs to have a number that's in between 90 and 100 so it could be done. We're not gonna do that here. So we may see some odd things such as that, and that's fine.
So we have grade score. Was there another column?
So we're are we are pretty much ready. We need to go back to our cedar
and we need
put in grade truncate. That way, when we rerun the seeder, it will empty the table when making new data. We need to use app
so that this file understands what a great ISS
and this one has dependencies on other tables. So we're going to use the concurrency function up against it.
And of course, we're going to change that to grade
let's make 100 different grades.
So let's go ahead and save that And let's run the cedar and see if it works for us.
So PHP artisan
TV. Colon seed.
No airs yet completed successfully. Let's go look at the grades
and we have grades.
They belonged to a course that belonged to a user.
Very good. Let's move on to the next factory
and the next factory is going to be.
Let's take a look at her database. Migrations.
We're on grades user types. Okay, This one shouldn't be too bad.
Let's go ahead and open up the user types table. Kind of refresh ourselves on what columns we had. We had title and description. Okay, that's pretty easy.
let's go ahead and make a factory.
I've hit up to go to the previous commands. I see the great factory I made. I'm gonna go ahead and
change this to user
and the model will be
I'll go ahead and hit. Enter. Let's go to our factories.
Come back to our database factories.
He's the type factory
on. The first thing we gotta return is a title.
So we're gonna do
I'm gonna say faker
Actually, we're gonna do unique, unique,
quotes and then array.
I'm gonna say, teacher,
comma. Let's see what else we need.
We need the description
where actually are not going to
create a meaningful description for these?
I was going to say
I guess we do something a little bit more creative with this. We could say
And we will put this up here.
I will say that this is equal to the result
and we'll say that
we've got changes to double quotes for the store.
curly brackets in there put the result in there. Now the description will mention the result that it's four
scored and save that.
Now we need to go back to our cedar,
and we, of course, need Teoh
truncate that table. So user type
And do we have a unique concern on our title?
I don't see constraints.
Um, uh, No, we do not. So we could put in teacher or student more than once, which would be bad. We usually probably want a constraint on that title so that we don't put in the same type more than once, But we're gonna leave that as is for now,
And it doesn't depend on any other tables. So we're gonna run it
without concurrency function,
and we're only gonna create too.
And we, of course, need to use that model
so that this file understands what that is
and let's see if we can run our cedar again.
PHP are Titian db colon seed.
Okay, we have an heir.
Let's see what the air says.
Okay, I see the problem. A four Grat. I forgot the semicolon at the end there
that will get you
and then it should work.
So let's run it again.
It says the database eating has completed successfully. So let's take a look at our Susan user types, data and D beaver.
Right? Click refresh.
This is the description for Student. This is a description for teacher.
Okay, that is done now, with that said, let's go ahead and move over to the summary for this lesson.
So that brings us to the 4.9 seeding the database to summary. And this lesson we reviewed and implemented the user factory. I had pre created that one for us. And then we went on to create and implement the great factory
and the user type factory. And we're almost done. We have one more factories implement in the next lesson. I hope to see you there. Thank you.