Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10

Video Transcription

00:00
Hi. Welcome back to Module three. Environment set up.
00:03
This is less than 3.7 modeling with lair bill.
00:08
Now, when we model with level, we're gonna use some PHP partition commands These commands willing to create a PHP object that represents a record.
00:18
An easy example is that our user model
00:21
links and represents the users table in the database.
00:26
We can also define relationships in the model.
00:30
This is separate from database constraints. Constraints still need definition in the database.
00:36
Some full effective elders Onley used relationships on this layer, and this is one reason you may find databases without the constraints implemented. It's because these full stack
00:48
frameworks make it easy to just do it on the application layer and then not do it on the data base layer. But you should always put in your constraints unless you have a good reason not to, and a good reason not to means that you will. You will have a very specific reason why you are not doing that
01:06
now. When we begin to use O r m
01:07
object relational modelling removing closer or deeper into object oriented programming.
01:14
Now we are going to go that deep into object oriented programming. We're just going to go deep enough to where we can easily generate fake database data so that we can test our database. Terrible also provides a neat tool for testing your models, and that's Tinker,
01:30
and we'll take a look at that in this lesson as well. With that said, let's move over to our virtual environment.
01:37
All right, let's go ahead and get started with some Larible modeling. Now, if you need to find the documentation on this component, all you need to do is Google Larible. Eloquent, eloquent is thesixties we're dealing with,
01:49
and
01:49
we need to make a model with a migration, which is going to be this command.
01:56
So to get started, we're gonna go ahead and get rid of the default users table that comes with it. Larible installation because they're not gonna be using it.
02:05
I also want to get rid of the password resets table,
02:07
and this is in the
02:09
shared database migrations, and there's one more file we need to get rid off, and that's in the APP.
02:15
We need to get rid of the user model. That's the that's the user model. The default user model that comes with narrow going and get rid of that.
02:23
And then
02:25
go ahead and exit out of this and I'm gonna clear the screen.
02:30
I'm gonna vag Grint Ssh! To open a shell into the bag Grant virtual machine that is running a boon to Lennox.
02:38
Then I'm gonna ls cd intolerable.
02:43
And then I'm going to do a PHP
02:46
partition make
02:49
model
02:51
user
02:53
migration.
02:57
All right,
03:00
when we saw that that user file popped back up in that directory
03:05
because that's the model and then it will also put a user migration file back in the database section.
03:13
So we go and we look at the data base
03:16
migrations.
03:19
Would you go ahead and we're gonna just make it very simple,
03:23
Very simple. Migration are user class is not going to be that complicated at all.
03:30
So, in fact, I'm just gonna just do table,
03:34
string,
03:37
name,
03:38
and so Michael and we want to get that syntax correct, because if we forget, the dollar sign would forget the semicolon,
03:45
it will crash and not run correctly.
03:47
And let's go ahead and hit the up arrow in our shell to go back to our last command. But we're not gonna make another user model. In fact, it would air out if we tried to, because it would say that it already exists.
03:59
We're gonna say, Look,
04:01
we're gonna say that users have books, so we're just going to say we're gonna make a book model with the migration.
04:15
Andi,
04:15
if we edit this file,
04:19
we're gonna go ahead and add another
04:24
a couple
04:25
columns. We're gonna keep it simple again. So name the name of the book
04:30
on a table.
04:33
It's a big manager
04:39
user. I d The user owns the book.
04:43
I'm gonna make that unsigned
04:47
because big increments is not really a data type. It's combining a few ideas into one command it saying it's a big manager, It's unsigned and it automatically increments. And that's so that we don't have to run those three commands independently
05:02
level package to the packages them together and this one command.
05:08
And then to make sure that we can
05:11
create a foreign key to that field, we need to make sure that it is of the same data type,
05:16
which is a big energy for use a regular energy. It would not work.
05:20
I was going to save that.
05:25
Let's go ahead and run a PHP. Our petition
05:33
migration
05:35
cool and fresh
05:39
That
05:41
my great fresh. Okay,
05:45
here we go.
05:46
Let's see what we got for an air.
05:49
So this air occurred because I recently changed my development space,
05:54
so actually need to go and at the password that level is trying to use in trying to use
06:00
and the dot m file. So database route
06:05
password is Sai Buri
06:09
i t.
06:11
And if I saved that and run the same command with up an inner,
06:16
you have a successful migration. Now, if I go into my
06:21
the beaver refresh,
06:24
I see my larible database,
06:26
I see my books
06:29
with the name and the user I d.
06:31
I see the users with the name
06:33
so we know how to set up a foreign key constraint in literal. We did that in the previous lessons. So we're just gonna find a relationship in our model class.
06:43
Let's go ahead and do that.
06:46
We need to go to the file that represents the model that's in the APP folder,
06:51
and we'll see these two
06:54
files, user and book that represent those models. So let's go into our user
06:59
table.
07:00
We're going and edit this. It's empty at the moment,
07:03
nice and simple,
07:05
and we're gonna add in our relationship for books. And the relationship relationship is going to be
07:11
that a user can have many books, so let's go ahead and enter that.
07:17
So the has many relationship definition isn't on this page, so we need to find it. So I'm just going
07:24
go back to Google and type in level has many
07:32
almost like we're on the right
07:36
page. So we're gonna one too many
07:40
and we can see that it's pretty simple.
07:44
This post comments has many
07:47
So Post can have. Many comments were, say, a user can has many books,
07:54
so let's go ahead and type this in. And but use our books, public function
08:01
books
08:09
we're going to return.
08:11
This
08:15
has many
08:18
on we're gonna say
08:22
up
08:24
book.
08:28
Go ahead and save that.
08:30
Now there is some magic that Larible is
08:33
performing for us in the background, for example, we haven't indicated which key
08:39
should affiliate this relationship, and that is because liberals making assumption. So we following. If we follow the name in semantics, that level expects we will not have any problems now if we change the naming semantics. Then label will start to have problems, but there are ways to tell arable that you've changed things,
08:56
but it's easier to try and just follow their
09:00
name in semantics if you can. And the name is semantics for this is that when we say this has many books, it's gonna look for the book model, and it's going to assume that the user
09:11
is part of the name of the column. So it's gonna look for a column name, User I D. And it automatically affiliate that in the relationship for us. That's why we're not having to indicate that anywhere
09:24
now to demonstrate the relationship working. Let's go ahead and add in a couple of dummy values. So we're going to users
09:31
going to Data and D Beaver. I'm going to click this plus symbol,
09:35
and I'm just going to add in a user of cyber a
09:39
and hit save. I'm not even gonna bother with the creator updated at.
09:43
Then I'm going to books.
09:46
I have two books
09:48
just to show that it's a one to many.
09:52
So again, the plus
09:54
I'm gonna say name,
09:56
book
09:56
one user I d. One because cyber a user is user. I d want to save that.
10:03
Go ahead and add in another record
10:07
and look to
10:09
He's already won again and save that.
10:13
Okay, so now we have Book one, a book to that's owned by user I. D. One We come and look at the user's table can see that he's already one is Sai Buri.
10:22
So let's go back into our shell
10:26
and let's type in PHP partition
10:30
Tinker.
10:33
And that's gonna that's gonna bring us into the Tinker program made by Justin Hillman Heilman. Think I said his name right?
10:41
And all we need to do is say, Let's say user
10:43
Colin. Colin,
10:45
find one
10:46
now. This is not saying, Find one or anyone. It's saying, Find the one with I D. One, which will be Sai Buri. So I'll hit. Enter
10:56
and sure enough, we got it.
10:58
Now let's do an arrow and save books.
11:03
And it returned both those books based on the user found so we could do this kind of stuff. Can say user variable, which starts with a dollar sign, equals user colon, colon, Find one.
11:16
Okay, so now user, is that assigned is that is that user that we found cyber, then we could say, OK, user, what books do you have by doing this?
11:28
And that brings back our books? And that will be really useful when we're seeding the database because we'll be able to go across those relationships, bring back the data and inserted into the related tables and follow those constraints that we create as well.
11:43
So that brings this section of this lesson to an end. Hope you were able to get all that modeling done. And if you have questions, the specific parts of the Larible documentation is directly related to what we're doing.
11:56
Specifically the migrations and the creating the model parts of you want to be careful because there are levels a pretty big framework, and
12:07
there are sections of it that we are not using at all. And it would be beneficial for this course to get lost in those sections.
12:15
So that finishes this section. It's time to head over to the summary,
12:20
and that brings us to this summary or the modeling with terrible summary. So what did we do in this lesson? Well, we created a level model with the migration file, which was the same type of file, but migration file. Anyway, it was the same type of father we actually made in the migration lesson. However, in this lesson,
12:37
we created a model with a migration file
12:41
and we also to find a relationship in that model. And we'd used Tinker to test that model.
12:48
So that brings this lesson to a close. And I will 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