Seeding the Database Part 2

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
or

Already have an account? Sign In »

Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10
Video Transcription
00:00
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,
00:19
and it will be available for download as a resource as well.
00:23
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.
00:40
I welcome back. So let's go over the changes that have already implemented
00:45
and the users table creation migration. I've added a couple fields. I've added the password field
00:52
and a Boolean activated field, and that is just because you will typically find these fields on user tables in data basis
01:02
and in the database cedar for the user factory
01:04
I've created, just like the previous two factor to factories that we created in the previous lesson.
01:11
And I've added faker calls to populate that with data
01:17
and in the cedar. I'm truncating the user
01:21
table and I'm running a factory on the user factory.
01:26
I'm not using the concurrency, and that is because the user table actually doesn't have any dependencies on other tables.
01:36
So we have tables that depend on the user to exist. But we don't have any fields on the user's table that
01:42
that's depend on existence in some alternate table, so we don't need to use that concurrency function in the cedar class.
01:55
And one more note. Remember to include your use app
02:00
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
02:19
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.
02:27
Make sure we are in the right directory, so I need a CD. It's level
02:31
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,
02:42
and then I'm going to hit up.
02:49
And I have my last factory call, which was the user factory.
02:53
Go ahead and change that to the grade factory.
03:04
There we go. Now, if you if you've added those fields to the user table as I did and you've
03:12
change your DB cedar to include the create statement,
03:15
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
03:35
jumped ahead to seed
03:37
fake data with the user data.
03:42
So let's take a look at what we have in our grade table. I'm gonna open up D beaver.
03:47
I wanna go to my grades.
03:51
Men go to the Properties tab.
03:53
I see that I have a user idea. Course I d letter grade grade score. OK,
04:00
so we need to get that factory open, and that's gonna be in the database factories
04:05
grade factory. I'm gonna edit that with no pad plus plus
04:10
and here we go. So first we need a user, I d.
04:15
So we need to rant role a random user.
04:19
Let's open up another factory where we're doing that, and we'll just change it to the user class.
04:26
Let's see, I think the room factory has one in it.
04:30
Yes, we're getting the building.
04:32
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
04:46
and the rest will work. And, of course, we need the use
04:50
AP
04:54
user at top. Otherwise, that won't work.
04:58
So then we'll drop down. We'll start our array of return. So it's a user
05:03
underscore i d
05:06
Big arrow
05:09
user
05:11
comma.
05:12
Next is the course I d. So I'm just gonna copy this
05:16
and paste,
05:18
and then I'm going again change the wording. So coarse
05:25
course
05:27
and again, we're going to need that use statement at the top to make sure that this file understands what of course is
05:33
okay.
05:36
and
05:40
try align those up there. So course underscore. I d
05:47
course.
05:49
That's in tax, right? We need those dollar signs. We need those commas. We need those quotes.
05:55
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.
06:04
Let her grade.
06:09
Yeah, Let her grave. Big arrow faker
06:15
random element. This is a function in faker.
06:19
We're gonna create an array. That's what the brackets on. We're gonna go a
06:25
be
06:27
see
06:29
the
06:31
death,
06:32
okay,
06:34
and comma.
06:36
And then we're going to do our course grade
06:40
a great score,
06:42
rain score,
06:45
Big arrow
06:46
faker.
06:51
And I've copied this from the faker documentation.
06:56
We're gonna get a random float. The Max Death Smalls
07:00
is gonna be too.
07:02
Our minimum will be zero and our max will be
07:08
100.
07:10
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
07:30
Okay, he rolled in A.
07:31
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.
07:43
So we have grade score. Was there another column?
07:46
No.
07:47
So we're are we are pretty much ready. We need to go back to our cedar
07:53
and we need
07:54
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
08:07
grade
08:07
so that this file understands what a great ISS
08:11
and this one has dependencies on other tables. So we're going to use the concurrency function up against it.
08:18
And of course, we're going to change that to grade
08:22
class and
08:24
let's make 100 different grades.
08:28
So let's go ahead and save that And let's run the cedar and see if it works for us.
08:35
So PHP artisan
08:37
TV. Colon seed.
08:41
No airs yet completed successfully. Let's go look at the grades
08:46
and we have grades.
08:48
Okay?
08:50
They belonged to a course that belonged to a user.
08:54
Very good. Let's move on to the next factory
08:58
and the next factory is going to be.
09:01
Let's take a look at her database. Migrations.
09:05
We're on grades user types. Okay, This one shouldn't be too bad.
09:09
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.
09:18
So
09:20
let's go ahead and make a factory.
09:24
I've hit up to go to the previous commands. I see the great factory I made. I'm gonna go ahead and
09:30
change this to user
09:31
type factory,
09:33
and the model will be
09:37
user.
09:39
I'll go ahead and hit. Enter. Let's go to our factories.
09:46
Come back to our database factories.
09:48
He's the type factory
09:52
on. The first thing we gotta return is a title.
09:58
So we're gonna do
10:01
title.
10:05
I'm gonna say faker
10:09
random elements.
10:11
Actually, we're gonna do unique, unique,
10:16
random element
10:20
quotes and then array.
10:24
I'm gonna say, teacher,
10:28
student
10:33
comma. Let's see what else we need.
10:35
We need the description
10:37
and, um
10:39
where actually are not going to
10:43
create a meaningful description for these?
10:46
I was going to say
10:50
this is
10:52
the description
10:56
four.
11:00
I guess we do something a little bit more creative with this. We could say
11:05
result,
11:05
people.
11:09
And we will put this up here.
11:13
I will say that this is equal to the result
11:16
and we'll say that
11:18
we've got changes to double quotes for the store.
11:26
But these
11:28
curly brackets in there put the result in there. Now the description will mention the result that it's four
11:33
scored and save that.
11:35
Now we need to go back to our cedar,
11:39
and we, of course, need Teoh
11:43
truncate that table. So user type
11:46
truncate.
11:50
And do we have a unique concern on our title?
11:54
I don't see constraints.
11:58
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,
12:13
So factory
12:16
And it doesn't depend on any other tables. So we're gonna run it
12:20
without concurrency function,
12:24
and we're only gonna create too.
12:28
And we, of course, need to use that model
12:31
so that this file understands what that is
12:35
user
12:35
type.
12:37
Save it
12:39
and let's see if we can run our cedar again.
12:46
PHP are Titian db colon seed.
12:52
Okay, we have an heir.
12:56
Let's see what the air says.
12:58
Okay, I see the problem. A four Grat. I forgot the semicolon at the end there
13:05
that will get you
13:09
and then it should work.
13:11
So let's run it again.
13:16
It says the database eating has completed successfully. So let's take a look at our Susan user types, data and D beaver.
13:22
Right? Click refresh.
13:24
Teacher Student.
13:26
This is the description for Student. This is a description for teacher.
13:31
Okay, that is done now, with that said, let's go ahead and move over to the summary for this lesson.
13:37
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
13:52
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.
Up Next
Intermediate SQL

This free course introduces the student to intermediate concepts found in the implementation and application of Structured Query Language (SQL) within professional business environments.

Instructed By