Playing
04. Creating a personal team when registering

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
Because in our entire application we always need to be inside of a team, it's a good idea that when registering we create a personal team for a user which they can later delete. So the flow of signing into an application would be we register an account, we get a personal team created, we could create an additional team, invite people to that team, and then we could just delete our personal team. It
00:22
just makes it easier rather than having to have a bunch of checks to check if we have an initial team or not. So in this episode we're going to, when the user registers, create their own personal team and then we're going to assign it to their teams, much like we've already looked at in our test. So how do we do this? Well there's a couple of ways, you could either bake this into your
00:42
registration flow, so for example over in the app HTTP controller section under auth we have the ability to register an account, so when we store an account we could do it somewhere in here, but I like to use observers for this because wherever this user is created we need a team for that user, for example if you create a user in your admin panel you're going to want a team associated with
01:07
that user. So let's go ahead and make out an observer, attach it to this user, and go ahead and create a team in that place. So we're going to make out an observer here and let's create out a user observer, we'll just put it in the default directory. So when we create out an observer, unless we provide in the model itself, in here it's pretty much just hooking into any eloquent events, so for
01:30
example we could say created and when that user gets created we get an instance of that user come through in here. Now for now let's just die dump that user and let's go ahead and write a quick test to make sure this is working. Now to attach a observer to a model we go over to the model, we go up to the very top and we can use the attribute here observed by, so we just pass in the
01:56
full namespace to this observer, so that's user observer in class. So now whenever a user is created this will trigger this method here and at the moment it's just going to die dump this out. So let's go ahead and create out a test here, so let's say phpr and pest test and we'll put this specifically in an observers directory, so let's say user observer because we're just testing the
02:19
observable behavior of this user. Okay let's open up the user observer test and let's fill this in. So the first thing that we want to check is it creates a personal team when a user is created, so we know at the moment if we were to just create out a user with our factory this should just die dump because our observer is hooked up to this model and this test should not fail but it should
02:47
just dump out a load of data. So let's go ahead and run just all of our tests and there we go we're just dumping that user out. Okay so how are we going to check this? Well we're going to create our user but let's specifically give this user a name because we're going to assume we're going to use the user's name that we create in the creation of the team. So let's write out the expectation
03:09
first and then we'll go ahead and fill in the functionality to get this working. So we're going to expect the user teams first of all to have a count of one so let's say to have count one then we'll grab the first team and we'll check the name is equal to maybe just the user's name or it could be Alex's team whatever you want to fill it in but I'm just going to create out the
03:37
team name as the user's name when they register. Okay let's go ahead and specifically run this test so let's go into feature and let's go ahead and pull in our observers and our user observer test and we know that's still just die dumping at the moment so let's go over to our user observer and see what we need to do here. So we know we have that relationship that teams relationship so much
03:58
like in the test that we wrote earlier to verify that teams can be attached we can just go ahead and attach this in here assign the value so we can extract any information out in here that we want to so let's say team create and what do we want to pass through to the name to create here well that's just going to be the name of the user as we've defined in our test so now that we've
04:18
done this this should just make sure we pull in the model name for team create out that team for that user let's go ahead and run our tests and find out and yeah it looks like it's failed so let's just have a look here and yeah this is just because we need to either unguard our entire team model or provide the fillable fields so let's go over to our team model which we haven't touched
04:40
yet and i'm just going to go ahead and unguard this whole thing which i tend to do so we're going to set guarded to false or you could set fillable and then choose the columns that you want to fill okay let's go ahead and run our test and yeah we get green so we can run all of our tests now and this looks like it should be working but it's not so we've actually come across an
05:00
issue where our observer is now getting in the way of our test which is a pretty common issue here so it's up to you what you do here you can either delete the original test that we created so let's just head over to that model test here you can either get rid of this or you could adjust this to be two now i don't think it really makes sense to go ahead and adjust this because really our
05:23
observer test is testing this same behavior so i'm actually going to go ahead and delete this original test that we created it doesn't really add anything it's not really that helpful so let's just end up with just one test here okay great so we now know that whenever a user is created we're going to automatically create a team let's just go ahead and test this out in the ui so let's
05:44
head over to our database and we'll just delete my user that i've created and let's go ahead and register another account here and we know now that when i go ahead and register this should go ahead and create a team let's hop over to the database and check this out and there we go we have a team with the name in here which of course you can adjust if you want
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!