Playing
01. Using HasManyThrough with a Pivot Table in Laravel

Episodes

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

Transcript

00:00
So Laravel doesn't have an immediately obvious way
00:03
to do a has many through relationship when you're working with a pivot table. We're gonna take the first part of this video just to look around how things are set up here,
00:12
what we're trying to do, and then I'm gonna go ahead and show you the solution. Okay, so we've got a user, obviously. This user can belong to many teams.
00:23
And because of this, if we just open up the user model here, the team's relationship belongs to many. So a user can belong to many teams and a team can have many users.
00:34
This is a pretty standard thing. You probably already have something like this set up in your apps. Now, the problem we've got here
00:42
is that we have a projects table. Now, this lists out all of the projects that have been created for a specific team. These don't relate back to the user.
00:52
So our goal here is to get all of the projects through the teams that the user belongs to. Now, we can't just quickly do that natively with a belongs to many relationship.
01:05
Let's go ahead and try it out first of all, and then I'll show you what I mean. So at the moment on the homepage here, I'm just going ahead and listing
01:12
through all of the user's teams just to demonstrate that we have these teams. But our goal, of course, is to get all of the projects. So what would we do here?
01:19
Well, our first thought would probably be to implement a relationship on the user called projects and grab it that way. So let's go over to our user model
01:29
and let's go ahead and create out a relationship here called projects. And let's just do what we think we should probably do in this scenario,
01:37
which is a has many through relationship. And we would say, well, a user has many projects through a team like this. Now, the problem with this
01:47
is that a team isn't associated to a user. It's done through this pivot table. So this relationship does work, but only if this team had a specific user ID.
01:57
And that wouldn't make sense in reality because obviously we would have multiple members of a team. So it has to be a belongs to many relationship with the pivot table.
02:08
Okay, let's go over and see if this works. If we give this a refresh, yeah, sure enough, we haven't got a user ID column in that team's table. So what do we do?
02:17
Well, what we need to get this relationship working with a project through many related models is a model. We need to pass a model through as the second argument here. So what is that model gonna be?
02:30
Well, it's gonna be a model still, but it's gonna be a model that represents our pivot table. So as well as in Laravel being able to make out a standard model,
02:40
we can make out a pivot type model. So we're gonna call this team user. So it maps up with the order that we've got these different resources in.
02:50
So if we go ahead and create this out, let's see what this has done. And you can put this anywhere by default. This will just pop this directly into the models directory,
02:58
but of course you could split it out into a separate folder. So we don't need to touch this because really what we've now got is a model that just represents this pivot.
03:07
And you can see that it extends pivot here rather than extending the base model like all of our other models do. But what we can now do is we can say,
03:17
well, the user has many projects through this pivot table. So we're gonna go ahead and say team user, and that isn't quite gonna work. We've got a couple of other configuration options to do.
03:31
So let's go over and see the state of this. And if we give it a refresh, you can see that we've got an unknown column projects dot, and there's just nothing there.
03:38
So we need to provide a few other options to has many through to get this working. So the first key that we need to provide is the user. So the model that we're currently working with
03:50
is in the pivot table. We know that that's user ID. If we just go over to team user, this is what we're referencing just now.
03:58
The second key is the one that relates to the team that we're looking at in this case under the pivot table as well. So this is also part of our pivot table.
04:07
Both of these just map up this relationship within that pivot table. The fifth key that we want to provide here is the local key for the model we're currently working in
04:18
that maps up to the user just here. And obviously that's ID. We have a user ID, which maps up to the user ID inside of that pivot table.
04:26
The last key here is the one that we want to map this up inside of the projects table that we have just here. So we know of course in the database that only contains a team ID.
04:38
Okay, now that we've done this, let's go over and give this a refresh and you can see sure enough, it kind of looks like it's worked.
04:44
So let's demonstrate this by going over to home, duplicating this down and going and trying to list through all the projects that we have through all of the teams that we belong to.
04:55
So let's go ahead and output the project title in here and let's have a look and see what we've got. There we go. So we've now fetched all of the projects
05:05
that we can access, but we've done that through all of the teams that we belong to inside of that pivot. You might not be using exactly the same example here
05:15
with teams and projects, but if you follow this pattern, you're pretty much going to be able to map up anything you need with data through a pivot table.
1 episode 5 mins

Overview

How do we set up a HasManyThrough relationship through a pivot table in Laravel?

While it’s not immediately obvious, it’s still possible by using a pivot model and referencing the right columns!

In this snippet, we’ll look at an example of accessing all projects through all teams that the user belongs to.

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

Episode discussion

No comments, yet. Be the first!