Download the PHP package oliuz/teamwork without Composer
On this page you can find all versions of the php package oliuz/teamwork. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download oliuz/teamwork
More information about oliuz/teamwork
Files in oliuz/teamwork
Package teamwork
Short Description User to Team associations for the Laravel Framework
License MIT
Homepage http://github.com/oliuz/teamwork
Informations about the package teamwork
Teamwork (Laravel Package)
Teamwork is the fastest and easiest method to add a User / Team association with Invites to your Laravel project.
Contents
- Installation
- Configuration
- User relation to teams
- Models
- User
- Usage
- Scaffoling
- Basic concepts
- Get to know my team(s)
- Team owner
- Switching the current team
- Inviting others
- Accepting invites
- Denying invites
- Attach/Detach/Invite Events
- Limit Models to current Team
- License
Installation
For Laravel ^8.0|^9.0
"oliuz/teamwork": "^9.0"
Add the version you need to your composer.json. Then run composer install
or composer update
.
(or run composer require oliuz/teamwork
if you prefere that)
The Teamwork
Facade will be installed automatically within the Service Provider.
Configuration
To publish Teamwork's configuration and migration files, run the vendor:publish
command.
This will create a teamwork.php
in your config directory.
The default configuration should work just fine for you, but you can take a look at it, if you want to customize the table / model names Teamwork will use.
User relation to teams
Run the migration
command, to generate all tables needed for Teamwork.
If your users are stored in a different table other than users
be sure to modify the published migration.
After the migration, 3 new tables will be created:
- teams — stores team records
- team_user — stores many-to-many relations between users and teams
- team_invites — stores pending invites for email addresses to teams
You will also notice that a new column current_team_id
has been added to your users table.
This column will define the Team, the user is currently assigned to.
Models
User
Add the UserHasTeams
trait to your existing User model:
This will enable the relation with Team
and add the following methods teams()
, ownedTeams()
currentTeam()
, invites()
, isTeamOwner()
, isOwnerOfTeam($team)
, attachTeam($team, $pivotData = [])
, detachTeam($team)
, attachTeams($teams)
, detachTeams($teams)
, switchTeam($team)
, isOwnerAuth()
, isOwnerAuthCheck()
within your User
model.
Don't forget to dump composer autoload
Middleware
If you would like to use the middleware to protect to current team owner then just add the middleware provider to your app\Http\Kernel.php
file.
Afterwards you can use the teamowner
middleware in your routes file like so.
Now only if the authenticated user is the owner of the current team can access that route.
This middleware is aimed to protect routes where only the owner of the team can edit/create/delete that model
And you are ready to go.
Usage
Scaffolding
The easiest way to give your new Laravel project Team abilities is by using the make:teamwork
command.
This command will create all views, routes, model and controllers to make your new project team-ready.
Out of the box, the following parts will be created for you:
- Team listing
- Team creation / editing / deletion
- Invite new members to teams
- Team model
Imagine it as a the make:auth
command for Teamwork.
To get started, take a look at the new installed /teams
route in your project.
Basic concepts
Let's start by creating two different Teams.
Now thanks to the UserHasTeams
trait, assigning the Teams to the user is uber easy:
By using the attachTeam
method, if the User has no Teams assigned, the current_team_id
column will automatically be set.
Get to know my team(s)
The currently assigned Team of a user can be accessed through the currentTeam
relation like this:
The Team
model has access to these methods:
invites()
— Returns a many-to-many relation to associated invitations.users()
— Returns a many-to-many relation with all users associated to this team.owner()
— Returns a one-to-one relation with the User model that owns this team.hasUser(User $user)
— Helper function to determine if a user is a teammember
Team owner
If you need to check if the User is a team owner (regardless of the current team) use the isTeamOwner()
method on the User model.
Additionally if you need to check if the user is the owner of a specific team, use:
The isOwnerOfTeam
method also allows an array or id as team parameter.
Switching the current team
If your Users are members of multiple teams you might want to give them access to a switch team
mechanic in some way.
This means that the user has one "active" team, that is currently assigned to the user. All other teams still remain attached to the relation!
Glad we have the UserHasTeams
trait.
Just like the isOwnerOfTeam
method, switchTeam
accepts a Team object, array, id or null as a parameter.
Inviting others
The best team is of no avail if you're the only team member.
To invite other users to your teams, use the Teamwork
facade.
You can also send invites by providing an object with an email
property like:
This method will create a TeamInvite
model and return it in the callable third parameter.
This model has these attributes:
email
— The email that was invited.accept_token
— Unique token used to accept the invite.deny_token
— Unique token used to deny the invite.
In addition to these attributes, the model has these relations:
user()
— one-to-one relation using theemail
as a unique identifier on the User model.team()
— one-to-one relation return the Team, that invite was aiming for.inviter()
— one-to-one relation return the User, that created the invite.
Note:
The inviteToTeam
method will not check if the given email already has a pending invite. To check for pending invites use the hasPendingInvite
method on the Teamwork
facade.
Example usage:
Accepting invites
Once you invited other users to join your team, in order to accept the invitation use the Teamwork
facade once again.
The acceptInvite
method does two thing:
- Call
attachTeam
with the invite-team on the currently authenticated user. - Delete the invitation afterwards.
Denying invites
Just like accepting invites:
The denyInvite
method is only responsible for deleting the invitation from the database.
Attaching/Detaching/Invite Events
If you need to run additional processes after attaching/detaching a team from a user or inviting a user, you can Listen for these events:
In your EventServiceProvider
add your listener(s):
The UserJoinedTeam and UserLeftTeam event exposes the User and Team's ID. In your listener, you can access them like so:
The UserInvitedToTeam event contains an invite object which could be accessed like this:
Limit Models to current Team
If your models are somehow limited to the current team you will find yourself writing this query over and over again: Model::where('team_id', auth()->user()->currentTeam->id)->get();
.
To automate this process, you can let your models use the UsedByTeams
trait. This trait will automatically append the current team id of the authenticated user to all queries and will also add it to a field called team_id
when saving the models.
Note:
This assumes that the model has a field called
team_id
Usage
When using this trait, all queries will append WHERE team_id=CURRENT_TEAM_ID
.
If theres a place in your app, where you really want to retrieve all models, no matter what team they belong to, you can use the allTeams
scope.
Example:
License
Teamwork is free software distributed under the terms of the MIT license.
'Marvel Avengers' image licensed under Creative Commons 2.0 - Photo from W_Minshull