9 hours 41 minutes
I Welcome back to module four. This is less than 4.8 seeding the database one. And this lesson one to get started with seating the database, getting all that fake data into our development database so that we can
test much better than if we're trying to manually insert a record here and there.
So what we're gonna do, we're gonna insert a lot of fake data into our database is gonna be much better for testing. It's gonna be much faster. We can easily dump and regenerate data we can use Tinker to test faker calls. Faker is a library that provides us with a bunch of fake data so that we don't have to write functions that would generate fake data for us.
Ah, faker and method at calls can be found at the following u r l Now with that said, Let's get started. I Welcome back. Now let's get started with seating the database. I've gotten a head start that's so that we can fit this into two videos. But don't worry. We're going to walk through everything and explain every line of code
that's going on here.
So I have the database cedar dot PHP File open.
That's in the shared Database Seeds directory. And it's likely that when you open this file that you will only have this line in that file within your run function. And that's this line that is commented out with these two Ford slashes. And what that means is that it's not doing anything.
It's just there is an example.
It's it's a comment.
So when you run the database cedar, the primary thing that's gonna happen is that this function is going to run.
So the first thing that happens in this function is we have a DB statement set foreign key checks to zero, and that is so that our database does not complain when we delete all our data, we otherwise, when we try to truncate the tables in order to make new data,
the database would complain and say, You can truncate this data. It's related to other data.
So to alleviate that, we set this to zero, which is false, which means it's not occurring in the database. We then truncate
our database tables because I've done room in building. I'm truncating those tables in these two commands.
The next thing that occurs is we make a factory
command call and we pass it to building class and we ask you to create 20 of those.
And that's what this line does.
Now, once we start inserting data that is related to other data or has foreign key constraints, we need to use this concurrency function, which is this function down here.
So what this is going to do this is gonna pass the room class
as the class in the function and then do a factory create call on it one by one. So if we send it 100 like I am here, it's gonna go into afford loop
and cycle 100 times to effectively create 100 rooms. Now, the reason we do that is because if we take a look at the room factory,
we are getting in random order, order a building
and the first i d.
And then we're using that to create the model. So in the in this example, we can see that we were turning in name
that we use a
A class called faker to create a unique user name
again, we use figure to create a unique number between 25 100. And then we also say that building I d is going to be that random building that we generated at the top here. Now, if we did this the traditional way, which is just using this command,
what it would do is it would break the call into a bunch of threads and a bunch of depending on the computer. A bunch of
rooms would have the exact same building number, so it won't be truly in random order. You get a bunch of duplicates
and then the next item is that we set foreign key checks, the one so we turn our foreign key checks back on so that it works again.
So let's go ahead and run this and see what that looks like.
So here we are. I have a shell session into my virtual machine in Bagram.
Come and do a PHP partition DBC,
and we can see that it's completed successfully. So let's see what that looks like in the database.
So here are the rooms
me refresh. Um,
they changed again because I just receded it.
So this is all our random room data, and they belong to real buildings Because we have the foreign key here. We can see the different building names.
So I go looking buildings again. We have those 20 random buildings.
So we've generated random data for our buildings and our rooms.
Let's go ahead and start generating random data for our next table,
which looks like it should be.
Look at my migrations.
Course is Okay,
so I'm going to go back into my shell such session, and I'm gonna do a I'm going up to the command that I used before.
So this is a command. It's PHP are Titian make factory.
And this is the previous one room factory Dash him room and dash absence were model. What? We are doing the room right now, So I'm gonna call this one the course factory.
I could name it anything I wanted, but I'm gonna name it something that makes sense. It's for the model. Of course.
I would hit in her
and we go factory created. So what the dad do for us? Well, if we go into our folder structure, I'm going to the database and we look in the factories folder.
We'll see a course factory. Now, if I go ahead and open this.
We'll see that it's pretty much empty,
so we have to start adding that data.
So let's go look at our courses
and see how many columns we have. This one actually has quite a few.
So will be a good example. So
we don't have to worry about the 1st 1 The i d. It's an auto incriminating field, so room I d
and I'm gonna leave these blank for now.
So we got a quote it to
so we got to get the syntax right. We have to have the the quotes
you got that the commas. If you know, if we missed that, then of course it will fail and complain about it.
Onda started it
on and it
and we don't have to handle the
created at updated at or deleted at lower levels. Handles that by itself. Then, of course, will be dealing with that in the sequel code later on.
So I've these are all empty right now.
So a good idea
and spell it right
on. The commands we need is we need
and we need to just set the variable faker equal to a new instance of the faker with this command and it's common to have to look this kind of stuff up.
So now we have a faker
and since so I can type in this
faker arrow name
and I can see that what values I'm going to get this will let me test things.
So we have a course code.
It's gonna the faker website and see what kind of items we have to choose from.
So let's just search for code first. So postcode
No, we don't want this. We want to stuff at the top.
so it looks like we aren't going to get a, um,
Uh, example. So we'll just use a building number,
so I I contest this so I can put in building
Can I could see that I'm gonna get building numbers
now. What I'm also gonna need to do is type in the word unique
on a narrow.
And what this does is this is will ensure I don't get the same building number or code because we have a unique constraint
on that course code.
So we want to make sure that we don't use the same one more than once because the database will complain and crash.
So there is one example title.
Let's see if we just do faker title what we get
That's not what we want
that's close enough for me.
what we'll do is work.
Actually, I could just type that in
on. We'll do unique on this one as well.
Okay, Max. Students
gonna look over at what I used over here.
It's gonna copy this from my room factory
on. I'm just gonna leave the same
now, the start date
for the start in end date. We're going to use the date time between, and we're gonna adjust it so that they make sense.
Let me go ahead and copy this.
I'm gonna paste it,
go ahead and make it whiter.
So let's say
plus one year
and the end eight equals,
actually, let's change this. Let's change this one too. Now,
on the end date for this one year.
So from now to one year,
just to make sure that the end A doesn't start before the
start date which wouldn't make a lot of sense. We're going to make sure that this ends in the future.
So we're going to one year
and not These are quotes.
And then for this one, we're gonna do two years.
so now we need our
room, i d.
So we're gonna need to run role a random room. And we did that in the room.
So we're gonna
so we're gonna do room
equals room capital are on that one in random, or everything else is fine.
And then we'll drop this down into that 1st 1
Now, we're still missing something we need to use.
room. We have to have that resource in this file. Otherwise, this file will not know what a room is. It will complain when it gets to this line. By including this line of code which says you zap room, it becomes aware of what a room is. So that's an important piece right there.
So when working with these files, of course, what you want to do is make sure that you're getting the syntax correct. Make sure you have your semicolons, your commas,
in the appropriate places. Because violating the syntax will lead to a crash when you go to see
Okay, So I think we're ready to run the course. Let's go ahead and put it here. We need to do a concurrency.
And this, of course, is gonna be course.
Now that we've included course in a file, what do we need to dio? We need to make sure this file knows what that ISS. So we need to use APP course
with a Capital C.
And also let's truncate the course
this way when we run the cedar again,
we won't just keep adding two courses, which can cause failures when we're using unique in the faker.
Because the faker will not realize that we already have that name out there or that code out there.
So let's go ahead and run this and
check that it works.
I know this lessons getting along,
so it's gonna be PHP are Titian
D B colon seed.
men student doesn't have it. Default Follow you.
Okay. The men students
Did we put that in there? We have Max students. We don't have men students.
We will just
at it like so
I will say one
Ron R cedar again.
It is successful. Let's check D Beaver.
So we did courses, data
There are all the courses now
with all our random data. So we're on our way.
So this course is getting long on the long side, and I apologize for that. And it does have quite a bit of information in it that we went over. Let's go ahead and head over to the summary, and then we'll move on to the next course from there.
So that brings us to the 4.8 seeding the database. One summary realized this lesson was getting a little bit long, but there was a lot of good information in there. We really got started with seating our database and the next couple of lessons we're gonna finish. We're going all the way to the end and make sure that our whole database gets seated.
So in this lesson, we reviewed the building and room factories that I got a head start on. And then we created the course factory, and the next lesson we will create even more factories. Oh, and I hope to see you there. Thank you.