This episode is for members only

Sign up to access "Laravel Teams" right now.

Get started
Already a member? Sign in to continue
Playing
37. Getting all related models through all teams

Episodes

0%
Your progress
  • Total: 4h 36m
  • Played: 0m
  • Remaining: 4h 36m
Join or sign in to track your progress
01. Introduction and demo
4m 49s
0%
02. Setup with Pest
4m 2s
0%
03. Building the user teams relations
6m 5s
0%
04. Creating a personal team when registering
6m 3s
0%
05. Leaving all teams when an account is deleted
3m 55s
0%
06. Tracking the current team
4m 50s
0%
07. Showing team details in the UI
2m 55s
0%
08. Switching to another team
7m 24s
0%
09. Authorising team switching
6m 53s
0%
10. Updating a team name
10m 48s
0%
11. Basic roles and permissions setup
5m 26s
0%
12. Team roles and permissions middleware
9m 18s
0%
13. Authorising current team updates
3m 43s
0%
14. Testing team permissions through HTTP requests
3m 58s
0%
15. Leaving a team
10m 50s
0%
16. Displaying team members
8m 42s
0%
17. Making team members look better
5m 3s
0%
18. Removing a team member
15m 18s
0%
19. Preventing self removal from a team
4m 7s
0%
20. Storing invitations
13m 31s
0%
21. Validating invitations
6m 38s
0%
22. Authorising team invitation creation
6m 22s
0%
23. Displaying invitations
3m 16s
0%
24. Revoking invitations
12m 46s
0%
25. Sending an invitation email
13m 6s
0%
26. Accepting an invitation
12m 22s
0%
27. Displaying a modal to change a member’s role
10m 31s
0%
28. Updating a member’s role
9m 5s
0%
29. More authorisation and checks for role changing
10m 14s
0%
30. Fixing up the email sending test details
49s
0%
31. Fixing and validating email addresses for invites
1m 32s
0%
32. Tidying up @can directive checks
3m 25s
0%
33. Detaching roles when removing users
5m
0%
34. Adding an extra layer of protection to the team middleware
6m 35s
0%
35. Getting related models through teams
5m 37s
0%
36. Building a helper to access the current team
10m 47s
0%
37. Getting all related models through all teams
7m 15s
0%
38. Creating new teams
13m 7s
0%

Transcript

00:00
When we build systems where we can switch between teams it's often useful to get a high level overview of all of the models, in this case projects, for all of the teams that we belong to.
00:12
So just off camera I've gone ahead and added some projects to Mabel's team and I've also added Alex to Mabel's team as well. So as we're on Alex's team you see these projects here, if we switch over to Mabel's team we see a different list of projects of course because they belong to different teams. But what we want to achieve now is another box on the dashboard that shows us all the teams or all the projects that we have amongst all of the teams that we belong to. Now this sounds like it might be a pretty easy task, surely we could just go over to the user model because this relates to the user specifically,
00:48
create out a project relationship here and then return this has many through like so, and we want to say well we have many projects through all of the teams that we belong to. Now unfortunately this doesn't work purely because we have a pivot model here. Has many through is designed to work through intermediate models but only through a has many type relationship not through a belongs to which involves a pivot table.
01:19
So we'll stick with this for now and then we'll go ahead and figure out how we do this. So let's go over to the dashboard controller and let's create out a new thing to pass down here and we'll say all projects, and for this we can't use the team helper because remember this doesn't relate to a specific team. So we're going to have to go ahead and bring in our request or use the auth helper or auth facade.
01:40
So let's say request user and we'll say projects. Okay great so we would expect that to work, of course it doesn't because there's no user ID within the teams table. We don't have an assignment here we have a pivot table linking these two things because many people can belong to many teams and many teams can have many people. Okay so how do we do a has many through relationship using a pivot table?
02:07
Well let's get started by going ahead and making out a model here called team user. So that's basically a copy of what we have here team user but this model will actually be a pivot model. So if we use the pivot flag let's take a look at what that's done. It's been put in the same directory as our models but this now represents our pivot table.
02:32
So you can see that rather extending model it extends pivot. Now we don't need to put anything inside of this model we're just using it as a reference point for our relationships. What we can now do though is now change over this project relationship to use a has many through, but provide all of the arguments that we need here to get this mapped up.
02:53
So let's go through this one by one and see what we need to do. So what are we getting through many things? Well we're getting our project so that can stay exactly the same. Now rather than say through our team specifically which remember relates to our teams table we're now looking at that through the pivot table. So what we need to do is use that team user pivot inside of here instead.
03:20
Now because of this what we now need to do is provide in some additional keys to tell Laravel or Eloquent how to map these up. So we need to provide the user key that exists within the team user pivot table. That's pretty obvious we know that that's just user ID. Now we need to provide the foreign key on the projects tables.
03:40
This is the second key here. The foreign key on the projects table we know is the team ID. It doesn't relate to a user specifically. So when we create our projects out we have a team ID rather than a user ID. Next up is the local key for the user. We know that a user just has an ID to be able to map it up to this. And the last key is the foreign key on the team user pivot.
04:04
And that's going to be the team ID. So we've got this relating to team ID. This relates to the projects table. This relates to the users table. And this relates to the pivot table as well. So these relationships can get pretty complicated. But if you go ahead and just experiment with them you can come up with something like this.
04:23
OK now that we've done that let's go over and give that a refresh. And it looks like everything is OK. So let's just start to dump this out. So if we head over to our dashboard controller let's just take these projects and just dump them before we return that view. Now we will expect here to see six projects because we have over here three projects and three projects for Mabel as well.
04:44
So let's go over and see what we get. And yeah sure enough we have got six items in here. Let's iterate through these and just make sure everything looks good. So over on our dashboard view here let's just take this container and duplicate it down. Let's add a little bit of spacing between these things.
05:03
And let's open up this second one and let's change this to all projects. So let's go and iterate through all projects as project and we'll just output the project title as well. Let's give that a refresh and there we go. So we've now got an overview of all of the projects that we have within the system regardless of which team they exist within.
05:23
Now what we can do to further make this a little bit better is output in here the team that they belong to. So we don't have this relationship set up yet but we can just say project team and then the team name. So if we open up the project in here and we just go and create that relationship out. And we're going to need to be careful here with egoloading so we'll fix that up in a second.
05:45
But let's create out this simple belongs to relationship back to the team from the project and we should be good. So let's say team class and let's open this up and there we go. So now we get an overview of all of our projects. Mabel only sees three because she's not part of Alex's team but I see a list of all of them because I'm part of Alex's and Mabel's team.
06:07
OK so the moment when we start to do things like this which we haven't done throughout the course we're going to end up with an egoloading issue where we're going to get multiple queries here. So let's just do a composer require here on the Laravel debug bar package and that should instantly show up for us in the browser. And we can take a look here at how many queries we're running and you can see here for every single project when we access that team we're running a few too many queries. So what we can do to fix this up really really simple over on our dashboard controller we can say request user projects.
06:41
We can say get which shouldn't alter things at all so if we just have a look here we still get 13 queries. But now that we're doing things like this we can say with and teams that will egoload the team for all of the projects in and sure enough we end up with only eight queries here which is about right and we don't have any duplicates in here either. OK great so there we go that's how just a little tip to get lots of records through intermediate relationships but when you are using a pivot table. Super helpful for a system like this.
38 episodes4 hrs 36 mins

Overview

Need team functionality in your Laravel application? Let’s build it from scratch.

We’ll cover the basics of creating teams, switching between them, sending secure team invites by email, and managing team members.

Powering everything will be roles and permissions for each member, with the ability to switch roles directly from your team dashboard.

Once you’re done, you’ll have mastered team functionality in Laravel.

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

Episode discussion

No comments, yet. Be the first!