A laravel package from Codelab that handles team management business logic onbehalf of your laravel application. It can be used with any UI framework.
The main purpose of the package is to provide resource sharing as well as compartmentalization of the shared resources.
To add the package in your laravel project run the following command.
composer require codelabmw/teams
After the installation, run the following command to publish the migration and config files.
php artisan vendor:publish --tag=codelab-teams
And then run the following command to create necessary tables.
php artisan migrate
To start creating teams, in your model acting as a team entity (the model of your choice to have teams) add the HasTeam
trait.
<?php
...
use Codelab\Teams\Traits\HasTeam;
class User extends Authenticatable
{
HasTeam;
...
}
Once you have your entity configured you can then create teams using the teams relationship method or the provided helper method.
-
Using the team's entity
teams
relationship method.$team = $user->teams()->create([...])
-
Using the team's entity
createTeam
helper method.$team = $user->createTeam([...])
The create
or createTeam
functions expects the following fields.
name: The team name. Required.
slug: Usually the name in lowercase and hyphanated. Optional.
description: An optional description of the team. Optional.
status: An integer between 1 and 2 indicating whether the team is active or inactive respectively. Defaults to 1 (Active). You can the Status enum provided by the package.
You can find a comprehensive list of helper methods here
To add a member to a team first you have to add the IsMember
trait to your model acting as a member entity.
<?php
...
use Codelab\Teams\Traits\IsMember;
class User extends Authenticatable {
IsMember;
...
}
By default the package will assume that the App\Models\User
class is the member entity. This behavior can be changed in the config file, specify your own member entity class on the member
attribute.
<?php
return [
'member' => Your\Custom\Entity::class
];
Once you have your member entity configured, you can proceed to add members to a team by using either of the following options.
-
Using the team's object
members
relationship method.// $member = new Your\Member\Entity $team->members()->attach($member);
-
Using the team's object
addMember
helper method.$team->addMember($member->id)
The
addMember
method requires the members id as an int or string. -
Using the member's object
joinTeam
helper method.$member->joinTeam($team->id);
The
joinTeam
method requires the teams id as an int or string.
You can find a comprehensive list of helper methods here
Because the package does not know how many and what kind of resources your application will have, things have to be done manually. Following are the steps to adding resource sharing.
First add the IsResource
trait to the resource entity (model class acting as a resource) in your application.
<?php
...
use Codelab\Teams\Traits\IsResource;
class Task extends Model {
IsResource;
...
}
Secondly extend the base Codelab\Teams\Models\Team
class in a custom team class.
<?php
...
use Codelab\Teams\Models\Team;
class CustomTeam extends Team {
...
}
To let the package know of the new custom class, change the team
attribute in the config file.
<?php
return [
'team' => Your\Custom\Team::class
];
And then finally create a relationship between the resource and the team in the new custom team class using the morphByResource
method.
<?php
...
use Codelab\Teams\Models\Team;
class CustomTeam extends Team {
/**
* Defines a has many tasks relationship on team.
*
* @return MorphToMany
*/
public function tasks(): MorphToMany
{
return $this->morphedByResource(Task::class);
}
}
Once all this is done you can go ahead to add resources to team objects using the relationship method you just created.
$team->tasks()->create([...]);
$team->tasks()->attach($task);
createTeam([
'name' => $name // required,
'slug' => $slug // optional,
'description' => $description // optional,
'status' => $status // either 1 (active) or 2 (inactive) defaults to 1,
]): Team
teams(): MorphMany
findTeam(
$id // required.
): Team
deleteTeam(
$id // required
): void
hasTeam(
$id //required
): bool
members(): MorphToMany
addMember(
$id // member id, required.
): void
removeMember(
$id // member id, required
): void
hasMember(
$id // member id, required
): bool
memberTeams(): MorphToMany
joinTeam(
$id // team id, required.
): void
exitTeam(
$id // team id, required.
): void
isMemberOf(
$id // team id, required.
): bool
teams(): MorphToMany
Thank you for checking out the package, don't forget to share and 🌟 if you liked it 😁.