This episode is for members only

Sign up to access "Laravel Teams" right now.

Get started
Already a member? Sign in to continue
Playing
05. Leaving all teams when an account is deleted

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
Most applications give the option to delete an account and our Breeze starter kit is no different. We can go ahead and delete our account directly from here. Now when we delete an account we don't want to delete teams because we could have other users as part of that team. What we
00:14
want to do is detach ourselves from that team. So let's go ahead and build in the functionality again directly within our observer here and then we'll just write a quick test to verify this. So another eloquent event here is deleting. What we want to do is do this while the user is being deleted and not after they've been deleted because then we won't have access
00:34
to their teams and we'll probably just get an error anyway. So when a user is being deleted let's go and access their teams and just detach. That's going to remove them from all teams. That's pretty much all we need to do. Okay now that we've done this let's go and just write a really quick test to verify this and then we'll go ahead and do this directly in the UI. Okay so we can come
00:57
over to our user observer test for this and let's go and just copy this entire thing just so we have a little bit of boilerplate and let's change the name here as well. So removes all teams or team attachments when deleted. Okay so we'll create out a user in here we don't need to specify the name here because we're not really interested in that and then let's go ahead and delete the user.
01:21
So to do that we're just going to really simply say user delete. Now this user doesn't have any teams at the moment so we're going to need to attach some but let's just make sure that this works first of all. So let's go into our observers run our user observer test and yeah at the moment we get failed so let's just have a look detach does not exist. Okay so let's go over here and have a
01:42
look here and yeah we're accessing this as a collection it needs to be the actual relationship itself. Okay let's run that again and yeah we just get a risky test here because we're not really asserting anything in here at the moment. Now what we want to do is as part of this go ahead and attach some teams to this but we can do this at the factory level. So let's pull this down and
02:02
before we create this let's use the has method to pass in a bunch of teams and we can pass as many as we want in here. So let's create out a factory for our team and let's say times two. Now at this point we don't say create we just need to pass this in and that will be created along with the resource. Now what we also need to bear in mind is that we have an observer as well when we create
02:27
a user to go ahead and create a team. So at this point three teams are actually going to be created the two that we've specified here and the user's personal team. So there's a way to get around this and we'll have to do this quite often throughout the course we can do create quietly so this will ignore the observer that we've created so we will only now end up with two teams. Okay let's just
02:49
run that test again just to make sure everything is good and yeah let's have a look here of course we just need to put in the namespace for team and we should be good great. Okay so what's the assertion here? Well really simply because we're just testing this single user in isolation we just want to make sure that the team user database is empty we want to make sure there's no attachments
03:12
there. So to do this we can say assert database empty and we can just pass the database name which is team underscore user. There's a couple of other ways that you could do this but this works fine since we're just running this single user test in isolation. Let's go ahead and run our tests and there we go we get green. So I'm not actually going to bother going through the UI
03:32
over this but now we know that because we still have a personal team when we delete this account that user's team should be detached. Now once again it won't delete the team because other users might be part of that team and of course you could adjust this later so if there are no users left in the team you might want to delete that it's entirely up to you but we now know that that is working.
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!