Playing
03. Building the user teams relations

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
So to start with the most basic thing let's start the team model and then the relationship between users being in teams. This is going to be a belongs to many relationship because multiple users can belong to many teams. Okay so we'll start with the most basic let's go ahead and make out a model here for our team and we will not do team invite yet we'll do a team and we'll
00:23
create out a model and a factory alongside of this as well. So let's go ahead and create that out and let's head straight over to the migration so create teams table and let's fill this in. Now this can be as complex or as simple as you like for me this is just going to be the case of adding in a string in here for the team name but you could improve on this and add any details you
00:45
want to these are going to be any of the updatable fields that we create. Okay so now that we've done that let's go ahead and migrate this and for our team factory let's go over and make sure that we're generating a fake name for this so in this case we could just do anything let's say fake and let's just use a sentence here with two words we'll just keep this really simple. Okay so now that we
01:12
have got teams let's go and create out the relationship between users and teams by using php artisan make migration manually and we're going to create the team user table make sure t comes first because this is by default alphabetical and then we won't need to specify within laravel. Okay so let's go over to the create team user table open that up properly
01:37
and let's go down and fill this in so basically we just need to know that we have a foreign id which is going to be the team id and we'll constrain this to make sure that at the database level there is actually a team with this id and then we'll just do exactly the same thing for a user id so now that we've done this we can hook up any users to any teams in any way that
02:00
we need. Okay let's go ahead and run our migrations again so say php artisan migrate and it's now we can go over to our user model and actually attach the relationship here so we'll just do this all the way down the bottom here and we'll create out a team's relationship and we're going to say this belongs to many and then we're going to say team class give the full namespace to that and that's
02:24
pretty much all we need to do so now from a user we can just grab any of the teams that we have let's go ahead and write a quick test for this just to verify that we can belong to a team and then we can just quickly test this out in the browser. Okay so we'll go ahead and use php artisan pest test and let's create this out as a feature test because we're going to be hitting the database
02:46
we're not going to write any unit tests necessarily none of the functionality that we're going to be creating is that complex that we need to go into the unit level most of it's just going to be everything that we would find in the laravel framework so for this case I would just create our feature test. Okay let's go into models and let's create out a user team test specifically
03:07
okay so let's go over to that user team test and let's create this out so by default this gives us as if we were trying to make a request to a page but we're not doing that so we'll say it has teams and let's get rid of all of this so what do we need to do here well we need to create a user first of all assign them to a team and then we need to check that they belong to a team so let's
03:28
go ahead and use our user factory here we'll create out a fake user the user factory is already enabled for us so we don't need to create that manually we can find that just over here and that gives us all the details that we need about the user now we're going to go ahead and try and attach them to a team so let's go ahead and say teams and let's go ahead and attach in a team here using
03:51
our team factory that we have just created so team factory and create and we should be good let's go ahead and just run our tests here that fails so let's just take a look and yeah we just need to put the namespace in for that team let's run our tests again and there we go we just get a risky test because we're not asserting anything but now what we can do is just create out a simple
04:12
expectation like user teams and we can even do something like count if we wanted to and then we could say that we expect this to be one as an example you could do really anything here we basically just want to make sure that we can attach a team to a user and then see that details but what you could do is you could take the count away here and you could say expect user teams and then
04:39
count to be one so that would work in exactly the same way but then you could go in and just check the team name manually if you wanted to so you could say something like name to be and then you could extract the team name out of here from this variable so now we have access to that variable we can just take the team name directly from there and make sure the correct one was attached
05:03
now for this we'd also need to go first because of course a user can have multiple teams and if we go ahead and run our tests we get green now we're not going to be running too many tests like this i don't see the point necessarily if you've got the relationship set up then it's all good but this is the kind of test that you could write if you just want to make sure that at any point
05:23
this relationship changes or something goes wrong but we now are pretty confident via this user team test that we can attach teams to a user and via that belongs to many we can get the count just to verify that that's in there and then we can extract any of the information about that team okay so finally what we can do is just to test this out we'll head over to our web routes and
05:45
we'll just dump this on the dashboard just so we can see this visually so if we just say auth and user and teams we should when we head over to our dashboard see all of the teams that we belong to of course we haven't attached any in the database just yet but if we were to create a team and then attach it in this pivot table then we would have that team available
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!