This episode is for members only

Sign up to access "Eloquent Relationships By Example" right now.

Get started
Already a member? Sign in to continue
Playing
20. Defining many to many relationships

Episodes

0%
Your progress
  • Total: 4h 18m
  • Played: 0m
  • Remaining: 4h 18m
Join or sign in to track your progress

Transcript

00:00
Many-to-many relationships are slightly more advanced, but again, these are one of the relationship types that you're probably going to use as your app starts to grow and gets a little bit more complex. So the example that we're going to be running through in this section while we are discussing many-to-many relationships is a course having multiple topics or multiple subjects.
00:22
So that would be like the course that you are watching right now, and this will have maybe topics like Laravel database. It could even have a host of other topics. So the point that we want to get to is having some model like a course that has many topics. But then if we think about the relationship type, a topic would then have many courses.
00:44
So very quickly, we could build up some powerful functionality where not only can we see which topics a course have, we can also filter and maybe create pages out for specific topics that list all the courses underneath that. So we're going to be doing that as we go through as the practical example rather than covering that right at the end. OK, so let's get started here. I've got a fresh Laravel application set up.
01:08
I've gone ahead and run the migrations just as normal. Let's start out with a course model. So we'll just define this out in a normal way, a course that has a little bit of information attached to it. Let's go ahead and create our course model here with a migration.
01:23
And let's go over to the create courses table migration under database and migrations. And let's fill this in. So this isn't going to be attached to a user. It's not going to be attached to a topic.
01:34
You could do that if you had one topic per course. But in our case, that's not going to be how this is going to work. So I'm just going to go ahead and add in a string here with a title. I'm just going to keep it really, really simple.
01:46
So let's go ahead and run our migrations here. And we're going to go ahead and just fill these in manually in the database. So let's go ahead and say eloquent relationships by example. I like so.
02:04
And let's go ahead and create out another one in here. Let's call this learn inertia, which will have probably topics like view Laravel, et cetera. So we've got two courses in here. At the moment, we don't know what topics these belong to.
02:22
So what we need to do is create another table here which houses all of the topics. So let's go ahead and do that and see what that's going to look like. So we're going to create another model in our application. And that's going to be a topic.
02:36
And we will create a migration alongside of that as well. So we're going to go over to the create topics table migration. And once again, we're not going to relate this to a course at all. We're not going to add anything about a course in here.
02:48
These are going to be completely separate. So let's again go ahead and create out this in a really basic way. We'll just add a title for each of these topics. And probably for things like this as well.
03:00
In fact, for courses as well, you'd want to do this. But you'd also want to add in a slug so it can easily be identified in a URL. So I'm going to go ahead and make this slug unique. And we should be good.
03:12
So let's go ahead and run PHP Artisan Migrate on this. And now what we have are two different tables that can relate to each other through a many-to-many relationship, but don't know directly about each other. So if we were to fill in here Laravel and Laravel as the slug.
03:29
And let's just fill these in really quickly. So we've got Laravel and let's say databases, databases. And in fact, let's say eloquent because that kind of makes a little bit more sense to the topics that we've got. And let's also create out an inertia tag.
03:46
And let's create out a view tag and just change these over. Like so and view. Okay, great. So we have a bunch of topics.
03:56
We've got some courses that do relate to these topics, but we haven't attached these directly via these two tables. What we now need is an intermediate pivot table, which will link these two things up. So we can add as many topics as we want to either of these courses. And then the topics can have many courses.
04:16
So under Laravel, we're going to see this course. And under eloquent, we're going to see this course. And then so on and so forth. We can attach as many of these as we want.
04:27
The question is, how do we do this? Well, the first thing that we need to do is create out another table, which will link these two together. So what we're going to do is manually create a migration. We're not going to create a model for this.
04:41
You can create a special pivot class to deal with these, but that really only comes in when we look at more advanced usage. So we're going to go ahead and manually make out a migration, not a model. A model doesn't link these two things together. And we're going to go ahead and create out a table called something.
04:56
And we need to figure out what the name of this needs to be under Laravel's convention. So we are going to create a table that names both of these two tables, but has the singular version. So basically, we want a course topic table. And that means that we can hook up these two things together.
05:18
The reason that we're calling it course topic table is because C obviously comes first in the alphabet. And that is Laravel's convention. You can change this around, but I would always recommend to stick to the conventions. Just saves you a lot of headaches in the future.
05:34
But if you do need to customize the table name for any reason, we can do that. And we'll talk about how we do that as well. So we're going to create a course topic table. So course underscore topic.
05:46
And that is pretty much it. So let's head over to the create course topic table migration. And let's figure out what we need to include inside of here. Well, we need to know for every single row what the course, what course links up to what topic, and what topic links up to what course.
06:03
So really all we need in here, we can add more data in here, which we're going to be covering later in this section. But all we need in here is this hooked up to a course ID and a topic ID. So we're going to use our foreign ID in here that we've seen before. And that's going to hook up to a course ID.
06:18
And again, we can constrain that in the database to make sure that exists with that foreign key. And we're going to duplicate this down and hook this up to a topic ID. Again, keeping this same convention, the singular version with underscore and ID. So now inside of this table, if we just go ahead and migrate it, we have complete flexibility to attach any course to any topic.
06:41
So we can attach course one to topic one, course one to topic two, and we can add as many topics as we want. And likewise, the topics will then contain them courses. So we're going to manually do this in the database just now so we can define the relationship and see this working. But of course, later, we're going to look at attaching these directly within code, detaching and also syncing these as well.
07:03
So let's just add in here what makes sense. So eloquent relationship by examples is going to obviously be under the Laravel here, and we can get rid of these timestamps. We don't really need them, but let's go ahead and add these in anyway. I want to duplicate this down and course one needs to hook up to eloquent.
07:20
So the same course eloquent relationship by examples will have another topic. And then if we duplicate this down again, learn inertia needs to be hooked up to inertia. And let's go ahead and add in another one. So learn inertia also might cover the view topic.
07:38
So save that out. And there we go. That is our table being defined. So now this course can access what topics it has. And each of these topics can access what course comes underneath these.
07:50
So let's go ahead and define out the relationship here. So the first thing that we're going to focus on the course is themselves. So I'm going to head over to our web routes, first of all, and I'm just going to return a list of courses. So let's go under resources and views, create our courses table.
08:08
And we'll say index.blade.php. So, again, this is going to look very basic, but let's go and just pass this data down. So we want to grab all courses from our database. So let's say course get.
08:21
And we could scope that by latest as well if we wanted to. Let's make sure we pull the namespace in for that. And let's just pass the courses down to our view. And let's head over to that index file and let's just iterate through each of these.
08:35
So we're going to do a foreach on courses as course. And end that foreach just here. Inside of here we'll create out a div. Let's just go ahead and add some styling really quickly.
08:48
So I'm going to come over to app.css and I'm just going to add course class in here. And I'm going to set the margin on the bottom. The margin on the bottom is fine to just say 40 pixels, just to space these out a little bit. I'm going to add a class in here of course.
09:03
And if you do want to add these styles, you're just going to want to run an npm install and an npm run dev just so your styles get uploaded. Okay, so we've got our course now and I'm just going to output the course title here. And let's make this a header in here.
09:20
And we'll maybe bump that down a little bit actually. And let's head over and just see what this looks like. Okay, there we go. So we've got both of our courses here.
09:29
And what I want to do if we think about the UI of this, I want to show the course topics above these. So that's what we're going to end up outputting here. So let's go over to our course because for our course we need to find out what topics relate to this. So I'm going to go ahead and create our topics method much like we would do.
09:50
This time though it's not a has many. So it does have many topics. But if you think about it, a has many relationship would involve over on our topics defining our course ID for each of these. That would mean a lot of duplication of data.
10:03
You would have to create a new topic, a physical new topic for every single course that you created. So instead we're going to go ahead and return. And we're going to say belongs to many. So it's not a many to many method.
10:16
It's a belongs to many method. And in here we go ahead and give in the topic class, the fully qualified namespace of the topic class. And let's go ahead and see what happens. So I'm going to come over to our web routes.
10:29
Let's just grab the first course. So I'm going to say courses and first. And then we're going to access the topics on that and just see what we get. So we know that for the first course, which is learn inertia, we should see them two topics dumped out in here.
10:44
Sure enough, we get back a collection like we've seen for most of the other many to many or has many relationships. And you can see we've got a topic here for each one. So that is how easy it is to define out that relationship and get that data back. So now that we know that we've got a list of topics for each thing, I'm going to add a note in here to make sure we pull in eager loading.
11:05
Really important. And we're going to head over to our index file and we're going to define out the list of topics just up here. So we're going to have another for each inside of here. And we're going to save for each course. Topics as topic. And we're going to go ahead and output these. So let's end that for each.
11:25
I'm going to go ahead and create a div in here. It's obviously not the best styling, but it will do for now. And in here, I'll just create our span for each one. So we're going to say topic and we know that we've got a topic model now.
11:35
So we're going to say topic title. Let's go over, give that a refresh. And there we go. So for learn inertia, we've got inertia view. And for eloquent relationships, by example, we've got Laravel and eloquent. I'm actually going to move these just to the bottom.
11:49
Just makes a little bit more sense here just so we can see what we're doing. And there we go. That is our relationship. Now, if, for example, later on, we thought, well, I want to add another tag to this. For example, I might want to add the Laravel topic to learn inertia because we might be working with inertia under Laravel.
12:09
Well, that's not a problem. We can come over to course topic. We can create out a new record and we can say, well, the learn inertia course needs to have the Laravel tag or the topic. And we go ahead and add that in. We save this out. And if we come over, sure enough, that has been added.
12:25
So this relationship type gives you a lot more flexibility. And it means that you don't have to define out a specific course ID in here and tie the topic itself down to anything in particular. You could even later on create another many to many relationship type. For example, if you started a blog, you could reuse your topics.
12:46
And for each of your blogs, you could have a blog underscore topic table, for example. And you could hook topics up to blog posts as well. So that's the beauty of a relationship like this and how powerful it is. And obviously, it's not really that much more difficult to implement with some of the other relationship types we've looked at.
13:08
It's pretty much the same. Of course, you can customize this. You can customize the intermediate table as the second argument. So if, for example, you really wanted this to be called something different, like topic underscore course, you could go ahead and define that in there if you wanted to. OK, so this is all well and good at the moment.
13:25
But we have been manually inserting this data directly into the database, which is probably not something you'd want to do. Probably somewhere in your code, maybe in an admin panel. You're going to want to be able to list and hook up this data to a specific course. So in the next episode, we're going to look at how we attach these directly in code.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!