9 hours 41 minutes
I welcome back to muzzle Fourth. This is less than 4.10 seeding the database three. And this. Listen, we continue building factories, but we are on our last factory, and that is the user courses factory. Following this lesson, we will have all our factories, and we can easily see our entire database.
Hi. Welcome back. Let's get started with making that user course factory. That's the last factory we need to complete all our factory classes and be able to succeed our database completely.
So I'm in a shell session of my vag unbox.
Go ahead and make sure that I'm in the right directory. I am. I'm also switched to the root user with a pseudo s dash s. I don't have to do that because I am already that user. And also something to keep in mind is that the root user and the vagrant user or two different users and they don't share the same command history.
if you're looking for a command biting the up, he and you don't see it, it's possible that you may have used it as the other user.
So I'm gonna go ahead and hit up.
There's my last factory command, and it happens to be the right factory. So you know that I've done this before
and that's going to create the user Koris factory with a model of user course someone hit, Enter
and it's been successful. We saw the file pop into the directory at the shared database factories.
I'm gonna go ahead and open that up.
I'm gonna go ahead and paste in what we will be using here. But first, let's take a look at de Beaver and see what columns we have on the user course table that we will need to include.
So I go to that table and go to properties.
There's three big ones to use their I d user type I D and course I d.
Now we don't have this user function yet. We don't even have the user model yet, so let's go ahead and
copy and paste this into the top of that.
This will look very familiar. It's the same type of calling we were doing in the previous classes.
I got my getting my random user getting my random type, getting my rent. Of course,
this table has a unique constraint on it. So the unique constraint is user I D. User type and course I D.
So this function, which we haven't created yet, is going to make sure that we get a random course
that the user that was randomly chosen
is not part off.
So because we're using that to get the course I d. We actually do not need this one,
so we'll get rid of it,
And then we also need to add the using at the top.
Use app, user use, app, user type. So this file knows what those models are.
we need to write a sequel query that will get a random, not enrolled course of a user when supplied a user i d. So let's start writing that.
So what we're going to need is we're going to need the courses table. So let's select star from courses.
Let's run that, see what we get back, okay,
I di not in. So let's use a sub query and it seems like it would be the just the easiest thing to do,
So we need to select the I Ds, Of course, is that the user is enrolled in
so we would get that information from the user course table. So select
Say I d
user courses. I'll give it an alias of you. See
where you see
user, I d is equal to one that's hard coded right now. So we're gonna switch this so that its dynamic when we integrate it into our model of ah, user,
this is actually the wrong I d. That's the idea on the actual pivot table.
We're going to need the course I d.
so we're select star from courses where I d not in
select course ID's from user courses, where user i d equals one currently, but that will be switched.
So let's go ahead and run that.
We actually don't need everything. We need something specific. We need the i d. From the courses table, which will be the idea of the course.
We don't need a bunch. We just need one.
So let's make it random. So we're gonna order by. Rand,
make sure this is working.
We're gonna see the orders. Always switching, but we only need one. So we're gonna run the limit
And there we go. We're getting a random idea of a course that the user is not enrolled in.
So we need to put this into our user model.
We need to go into our folder structure
in ah Sai Buri shared app and open up user.
And in this we'll see that we have this function random, not enrolled course. And we're doing a return of db select d be raw.
We're using the curly brackets that pass in this I d make sure you use the double quotes. It won't work with the single quotes
and this is the result.
It's going to return an array. So we've got to go to the first element of the ray on Get the attributes of i D. Which is the column that we are returning.
And this refers to whatever model that we currently have. So when we roll a random user,
if we get random user one it This is user one. Forget user to its user too.
And these files will be available for download as well.
This function by itself is not too bad. It's pretty, pretty short, sweet to the point.
So if we go into one way to test this stuff, though.
Is he going to tinker?
So let's see. PHP artisan
So if you're if you're curious to whether or not something you've done in the model will work,
it's a good idea to go into tinker and test it out.
User, find one.
So that's user one.
And let's say
what did we call that function again?
We call it
random, not enroll. Of course
paced. We need these brackets at the end.
Here we go.
So let's go back and look at that factory.
So we're gonna roll a random user.
We're gonna roll a random type. We're gonna insert that user i d from the user arrow I d. Then we're gonna insert the type from the
user type in random order. Get command That occurs up here. Now, the difference here is that this one goes out further.
You can see that we're getting the first, but we go one arrow further and go ahead and get the I d. That's why we don't have to put arrow I d at the end of this one
and then on that random user we're calling that function. We just implemented the random not enrolled course so that we get a random course I d. That they're not enrolled in.
Now. If we see the database and we select too many,
um, user course and relevance, it's possible we could get a collision. Still,
um, where we run out off,
uh, courses that they're enrolled in. But
as long as you put it in a lot of courses and a lot of users, that shouldn't happen.
So let's go ahead and take a look at our cedar class
that's gonna be in database seeds
database cedar dot PHP.
So in this file, you can see that I've running the concurrency function with the user course and I'm making 100
and I've included my use at the top to make sure that this file knows what it is,
knows what the user course model is. And I'm also truncating that so that when I run a seed that it works,
or that it dumps the old data and creates the new data.
So let's go ahead and run that and make sure that it works.
Okay, says that it completed successfully, So let's take a look.
and it looks
like it is indeed working. We have teachers and students going into courses.
All right, so we're done with that, and that completes the seeding of it.
Okay, let's head over to the summary.
So that brings us to the 4.10 summary. And this lesson. We created our factories with implementation of the user course factory and that completes our seed factories. I hope to see you in the next lesson. Thank you.
The Microsoft Azure Test is a premium Cybrary assessment created by Interview Mocha. The exam ...
AWS Data Engineering
The AWS Data Engineering test is a premium Cybrary assessment test created by Interview Mocha. ...