>> So one of these, the finest features
of the Laravel framework is the Eloquent ORM
and as David pointed out, an ORM is an object relational mapper
which allows you to map data base rows to objects in php
and allows you to create associations
between these different tables really easily without having
to write a lot of SQL under the hood
and gives you a really nice syntax for searching
for an individual record.
So instead of saying, select, star, from courses
where something else you'd say, courses, find [inaudible]
or courses find any of the courses you're looking for
and it would determine what SQL supposed to be written
and execute it properly for you and give it all back to you
in an object which you can then display
on the screen real easily.
So the way that the eloquent RM works is to...the convention
that it uses is that if you have a data base table
which is pluralized so for example, courses,
you would create a class in the models folder call course
and that's all you'd have to do is say,
class course extends eloquent and it
under the hood automatically assumes
that what the table name is, what the ID is,
so as there's an auto incrementing ID and there's one
that comes with Laravel by default it's called user
and it uses some advance features called interfaces,
which we're not going to go over,
but you can see class user, extends eloquent,
and you can set the table name
if you're not following the convention
of the pluralized...having a plural table name
and a singular class name.
But we'll go ahead create one for given the example
from before, we'll go ahead and create one for courses.
So we'll do course dot php
and do class course extends eloquent.
And so I'm going to jump in the php my admin
and actually re-name this table to courses.
So now I have a course's table with the same example data
from the first catalog that David had mentioned and now
that this course is
in the model's directory we can go ahead and say, course,
limit 10 so we only want to get 10 records,
get and we'll say this is courses.
And so now we're going to go ahead
and let's create a better view to display this stuff actually.
So I actually created a blade template earlier
and I'm just going to go ahead and copy it over here just
to save, save a few minutes of time.
[ Background noise ]
Okay, so I created a,
in the view folder I created a courses dot blade
and so I'm going to do view make courses, and I'm going
to pass a variable courses with the...actually that's not going
to work, just a second, courses title.
Okay, so it didn't work because it says access denied
for user route at local host.
So can anyone tell me what they think might be the problem here?
So the problem is that I never told Laravel what...who I am
in trying to access the data base,
it doesn't automatically know that I am able
to access the data base I'm trying to query.
So in the config folder
that I had mentioned earlier there is a data base dot php
file and in here we need to say that we are user name, jharvard
and password crimson and now if we go ahead and do
that unknown data base, data base.
So the data base that we're trying
to pick is called courses [inaudible]
and this time it worked.
So what it's doing here is it is let me go ahead
and jump back to the controller.
So I did courses, limit ten, get,
so I just said I only want 10 rows in the data base,
fetch those for them, and put them into this courses variable.
So this course's variable is now an object
which contains the different columns
which I can access really easily in the template.
So I for each course as course...go ahead and zoom in,
and I just say course title or course description
and it goes ahead and outputs whatever the data base row
is there.
So as you can see its much simpler but easily to reason
with than having to write out the SQL by hand
and then figuring out how to, how to map it into an object
but where it gets really powerful is when, as David sort
of had mentioned earlier, when you start
to have different tables that you want to associate
with other, via the foreign keys,
you can have these objects do the heavy lifting
of the querying and figuring out what pairs up with what and how
to go ahead and render that.
So if we look at...let's go ahead
and jump back here really quick, so data base config how it got
in there [inaudible] skip this for now.
So the different types in my...different types
of relations by default some of the...there are a few more
than this but these are the 4 most common are a has one,
a has many, it belongs too, and it belongs to many relationship.
So you can sort of think of a has one as,
you know if you only have one of something,
so in this case an account, this is just really dumbed
down simplified example scheme
but an account only has one supplier in this case.
So there's a supplier ID and a supplier
so in the account's table there's a supplier ID
and a supplier itself has an ID and there's only sort
of a one to one relationship.
Whereas a...there's a belongs too
so an order could only be placed by a customer,
whereas a customer might have a bunch
of orders the order only belongs to a single customer,
you can't have 3 different people buying the same thing
in this example.
And then a has many, like I said the customer has many orders
but not the other way around and then belongs
to many is the more advanced relationship
that David was going over earlier where you sort
of join two tables via a intern table,
so in this case assemblies, parts is a join
of the assemblies and parts or an example
that we were given earlier,
the courses...the instructors were joined
by a course instructor join table and in this case,
and in Laravel's case,
it actually doesn't require a primary auto implementing ID
on the join table though you can have one,
it actually doesn't require one or for it
to work the way it does.
So let's go ahead and set up sort
of a few different objects based on the lay out of the data base
that we have right now and so we're going keep courses
as our base object but we're going to say that in looking
at the data base table, the course has many meeting
so it has many meeting times, it belongs to a department
and it belongs to both many instructors and requirements.
So there might be multiple requirements for the course
but requirements could be shared by different courses.
So let's go ahead and see if we can model
that a little bit using the eloquent models
so you've got...let's start with the instructor dot [inaudible]
so similar to before, we're going to go ahead and say,
class instructor, extends eloquent
and so this would assume as I said earlier,
that there is a plural of instructor instructors
and this is the table that it will be mapping to.
And now on the course, we'll go ahead
and say...we'll create a method
so the public function instructors,
the public...sorry I'm...yeah the public function instructors
and the way that Laravel sets
up relations is you return a this belongs to many instructor,
so this tells us now that the course is associated
with the instructor model, that's all we needed to do
to set up that relationship and now as we're
in this loop here we can say, so because it's a has...it belongs
to many relationships there can one or more instructors.
So its an array or it's an object that has more
than one instructor on it so we're going to also loop
over here and say for each course instructors as instructor
and then pick out the first name and the last name,
I believe those are the columns so first and last,
it could be first name, last name.
So first and last it's set up in this course catalogue
and let's go ahead and see if that renders
so instructors...yeah it looks like they are showing
up unless there's not one associated
so in this case there are no instructors listed
with that course but in all the other cases you see one
or more instructors as defined in the relationship.
So one thing that is sort of a gotcha sometimes with ORM's
like this is you go ahead and define a relationship
and you think okay it's fetching my data properly
as I'm looking here everything is rendering
on the screen all right, everything is okay,
it's doing all the SQL behind the scenes for me
but what's happening here is
when it first fetches the courses it doesn't know
that you're also going to be looking for the instructors.
So it fetches all the courses for you and then it goes
to display all this data in the, in the view right here
and then it gets to this for each course instructors
and it says okay I don't know what the instructors are
yet let me go fetch those
and it's doing this every single time that you are going
through the course loop.
So every single time that you're trying to display a course
on the screen it's sort of late to everything saying I need
to find what the instructors are for this row
and so it's doing n plus one query
so it's doing however many loops you have it's doing another
query whereas that isn't the most efficient way to go
about fetching and displaying your data.
So the way to fetch everything up front in Laravel is
to say course with instructors limit ten get
and now it knows it looks no different just to the eye
but instead of doing I guess it would be 12 queries
or so behind the scenes now it's only doing two or three.
So we can go, we can continue in that fashion and set
up just a few more relationships so what were some
of the other has many meeting.
So similar we set up a model called meeting,
class [inaudible] extends eloquent
and it should...everything should be taken care of here
if we look at the meetings table it's got an auto incrementing ID
it's plural so everything should work properly if we try
to display the meetings.
So let's go back to, we already explained this
and it was a new relationship...sorry it was a
has [inaudible] meeting, so let's look at the meeting table
and see what we might want to display here.
So let's just display...actually I have a template set
up for this, I'll just grab a little table
and paste this in right here.
So I'm going to display just some data
about the different course meetings and [inaudible].
See if I missed something here, meetings, I had it singular
so now we have sort of the table here
as to the different meetings, what the type are,
when it begins, ends etcetera.
So I can continue on in just setting up these relationships
but as you see it sort of abstracts the SQL from having
to be written by hand and it takes care of a lot
of the heavy lifting behind the scenes for you
when you create these associations based on the keys
that are set up properly in the data base.
As you are going through
and installing Laravel there's a lot...there's so much
to cover it's actually sort of difficult to try to see
which part to bite off and go over but there are a lot more
which we'll continue to discuss probably on Monday.
A lot more features to the eloquent ORM
and the different ways of saving data, associating data,
deleting things, creating the different scopes
for when you are querying inserting [inaudible] etcetera
so you can take a look through this and I think...
>> The process it will be
like then moving forward is once we've formed our core group,
four person groups once we decide
on the various groups projects are the way responsibilities
will generally be divvied up is one or two of you will sort
of go back to the drawing board and figure out all right
for whatever problem we're trying
to solve here are the entities we have to implement,
for instance an instructor or a core or some kind
of joining there of and you'll spec out the data base tables
as we did in the first half of today and then as Tim alluded
to here the next step in that process will be to come
up with the classes and you'll write a few files
that extend eloquent in this way using some of the
out of box functionality but then also adding maybe some
of you own functionality for instance get phone number
where you might nicely format the phone number based
on some standard theme and so forth and then all
that information will presumably be communicated to your one
or two or other...three other people
in your group saying all right you have these classes,
you have this functionality now start actually writing the logic
implement the controllers, meanwhile someone else
in your group will handle the views and so the blade templates
that Tim was alluding to someone will take care
of the [inaudible] experience and use your interface
and the drop down user and all that
and even though we've just begun
to scratch the surface here the fact
that Tim didn't write a single line of SQL and the fact
that he was sort of able
to automagically join these multiple tables is
where you really start to see things fairly powerfully.
So even though at first it's going
to absolutely be a learning curve because you sort of have
to learn how this particular framework works.
In the end adding some new table, adding some new feature
to of course a new catalogue and the like can take just minutes
to do and updating templates, just seconds
so it's really quite exciting what you can start
to do quickly.
So thanks so much to Tim on that cliff hanger, do take care
of the lottery form otherwise nothing to do
until you hear back from us in a day or so via e-mail,
we'll see you next week.