Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10

Video Transcription

00:00
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.
00:20
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.
00:32
So I'm in a shell session of my vag unbox.
00:36
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.
00:56
So
00:56
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.
01:03
So I'm gonna go ahead and hit up.
01:07
There's my last factory command, and it happens to be the right factory. So you know that I've done this before
01:11
and that's going to create the user Koris factory with a model of user course someone hit, Enter
01:19
and it's been successful. We saw the file pop into the directory at the shared database factories.
01:26
I'm gonna go ahead and open that up.
01:29
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.
01:38
So I go to that table and go to properties.
01:42
There's three big ones to use their I d user type I D and course I d.
01:57
Now we don't have this user function yet. We don't even have the user model yet, so let's go ahead and
02:02
copy and paste this into the top of that.
02:07
This will look very familiar. It's the same type of calling we were doing in the previous classes.
02:13
I got my getting my random user getting my random type, getting my rent. Of course,
02:19
this table has a unique constraint on it. So the unique constraint is user I D. User type and course I D.
02:24
So this function, which we haven't created yet, is going to make sure that we get a random course
02:30
that the user that was randomly chosen
02:35
is not part off.
02:37
So because we're using that to get the course I d. We actually do not need this one,
02:42
so we'll get rid of it,
02:44
save it.
02:46
And then we also need to add the using at the top.
02:53
Use app, user use, app, user type. So this file knows what those models are.
02:59
Now
03:00
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.
03:12
So what we're going to need is we're going to need the courses table. So let's select star from courses.
03:21
Let's run that, see what we get back, okay,
03:23
where
03:24
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,
03:31
So we need to select the I Ds, Of course, is that the user is enrolled in
03:38
so we would get that information from the user course table. So select
03:46
Say I d
03:49
from
03:50
user courses. I'll give it an alias of you. See
03:54
where you see
03:58
dot
04:00
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,
04:11
this is actually the wrong I d. That's the idea on the actual pivot table.
04:15
We're going to need the course I d.
04:17
Okay,
04:18
so we're select star from courses where I d not in
04:23
select course ID's from user courses, where user i d equals one currently, but that will be switched.
04:30
So let's go ahead and run that.
04:33
All right?
04:34
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.
04:43
We don't need a bunch. We just need one.
04:46
So let's make it random. So we're gonna order by. Rand,
04:55
make sure this is working.
04:58
We're gonna see the orders. Always switching, but we only need one. So we're gonna run the limit
05:03
one
05:06
And there we go. We're getting a random idea of a course that the user is not enrolled in.
05:14
So we need to put this into our user model.
05:18
We need to go into our folder structure
05:23
in ah Sai Buri shared app and open up user.
05:30
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.
05:41
That query.
05:43
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
05:50
and this is the result.
05:54
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.
06:02
And this refers to whatever model that we currently have. So when we roll a random user,
06:08
if we get random user one it This is user one. Forget user to its user too.
06:15
Okay.
06:17
And these files will be available for download as well.
06:20
This function by itself is not too bad. It's pretty, pretty short, sweet to the point.
06:28
So if we go into one way to test this stuff, though.
06:31
Is he going to tinker?
06:34
So let's see. PHP artisan
06:38
tinker.
06:40
So if you're if you're curious to whether or not something you've done in the model will work,
06:46
it's a good idea to go into tinker and test it out.
06:49
User, find one.
06:53
So that's user one.
06:54
And let's say
06:57
what did we call that function again?
07:00
We call it
07:02
random, not enroll. Of course
07:09
paced. We need these brackets at the end.
07:15
Here we go.
07:15
So let's go back and look at that factory.
07:17
So we're gonna roll a random user.
07:20
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
07:32
user type in random order. Get command That occurs up here. Now, the difference here is that this one goes out further.
07:41
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
07:48
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.
07:58
Now. If we see the database and we select too many,
08:03
um, user course and relevance, it's possible we could get a collision. Still,
08:07
um, where we run out off,
08:09
uh, courses that they're enrolled in. But
08:13
as long as you put it in a lot of courses and a lot of users, that shouldn't happen.
08:18
So let's go ahead and take a look at our cedar class
08:24
that's gonna be in database seeds
08:28
database cedar dot PHP.
08:33
So in this file, you can see that I've running the concurrency function with the user course and I'm making 100
08:41
and I've included my use at the top to make sure that this file knows what it is,
08:46
knows what the user course model is. And I'm also truncating that so that when I run a seed that it works,
08:54
or that it dumps the old data and creates the new data.
08:56
So let's go ahead and run that and make sure that it works.
09:07
Okay, says that it completed successfully, So let's take a look.
09:13
User courses
09:13
data,
09:16
and it looks
09:18
like it is indeed working. We have teachers and students going into courses.
09:22
All right, so we're done with that, and that completes the seeding of it.
09:26
Okay, let's head over to the summary.
09:30
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.

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

Instructor Profile Image
Kitt Parker
Instructor