PHP code example of litepie / teams

1. Go to this page and download the library: Download litepie/teams library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

litepie / teams example snippets


return [
    'model' => [
        'team' => \Litepie\Teams\Models\Team::class,
        'team_member' => \Litepie\Teams\Models\TeamMember::class,
        'team_invitation' => \Litepie\Teams\Models\TeamInvitation::class,
    ],
    
    'features' => [
        'tenancy' => true,
        'workflows' => true,
        'file_management' => true,
        'real_time_events' => true,
        'team_analytics' => true,
    ],
    
    'permissions' => [
        'auto_create' => true,
        'middleware' => ['team.member'],
    ],
    
    'invitations' => [
        'expires_after_days' => 7,
        'max_pending_per_team' => 50,
    ],
];

use Litepie\Teams\Traits\HasTeams;

class User extends Authenticatable
{
    use HasTeams;
    
    // Your existing model code
}

use Litepie\Teams\Actions\CreateTeamAction;

$team = CreateTeamAction::execute(null, [
    'name' => 'Development Team',
    'description' => 'Our amazing development team',
    'type' => 'development',
    'settings' => [
        'visibility' => 'private',
        'features' => ['file_sharing', 'workflows'],
        'limits' => ['max_members' => 50],
    ],
], $user);

use Litepie\Teams\Actions\AddTeamMemberAction;

// Add a member with specific role
AddTeamMemberAction::execute($team, [
    'user_id' => $user->id,
    'role' => 'member',
    'permissions' => ['view_team', 'create_posts'],
], $teamOwner);

// Invite via email
use Litepie\Teams\Actions\InviteTeamMemberAction;

InviteTeamMemberAction::execute($team, [
    'email' => '[email protected]',
    'role' => 'member',
    'message' => 'Welcome to our team!',
], $teamOwner);

// Set tenant context
tenancy()->initialize($tenant);

// All team operations are now scoped to the current tenant
$teams = Team::current()->get(); // Only returns current tenant's teams

// Create tenant-specific team
$team = CreateTeamAction::execute(null, [
    'name' => 'Tenant Development Team',
    'tenant_id' => tenancy()->current()?->id,
], $user);

// Team permissions are automatically tenant-scoped
$user->can('manage', $team); // Checks within current tenant context

use Litepie\Teams\Workflows\TeamWorkflow;

// Team lifecycle workflow
$workflow = TeamWorkflow::create();

// Available states: draft, active, suspended, archived
// Available transitions: activate, suspend, archive, restore

// Transition team through workflow
$team->transitionTo('active', [
    'activated_by' => $user->id,
    'reason' => 'Team setup completed',
]);

// Check current state
if ($team->getCurrentState()->getName() === 'active') {
    // Team is active
}

// Team-level permissions
$team->addMember($user, 'admin');
$team->addMember($user, 'member');

// Check team-specific permissions
if ($team->userHasRole($user, 'admin')) {
    // User is an admin of this team
}

if ($team->userHasPermission($user, 'edit_team_settings')) {
    // User can edit team settings
}

// Update member role
$team->updateMemberRole($user, 'moderator');

// Update member permissions
$team->updateMemberPermissions($user, ['edit_content', 'manage_files']);

// Upload team files
$team->attachFile($request->file('document'), 'documents', [
    'uploader_id' => $user->id,
    'title' => 'Team Charter',
    'visibility' => 'team_members',
]);

// Get team files
$documents = $team->getFiles('documents');
$avatars = $team->getFiles('avatars');

// File permissions
$file = $team->getFirstFile('logo');
if ($user->can('download', $file)) {
    return $file->downloadResponse();
}

// API routes are automatically registered
Route::middleware(['api', 'tenant.ce('teams.members', TeamMemberController::class);
    Route::apiResource('teams.invitations', TeamInvitationController::class);
});

// API Resources
$team = Team::find(1);
return new TeamResource($team);
// Returns: team data with members, files, permissions, etc.

// Listen to team events
Event::listen(TeamCreated::class, function ($event) {
    // Send welcome notifications
    NotifyTeamCreated::dispatch($event->team);
});

Event::listen(MemberJoinedTeam::class, function ($event) {
    // Update team analytics
    UpdateTeamMetrics::dispatch($event->team);
});

// Real-time broadcasting
class MemberJoinedTeam implements ShouldBroadcast
{
    public function broadcastOn()
    {
        return new PrivateChannel("team.{$this->team->id}");
    }
}

use Litepie\Teams\Testing\TeamTestHelpers;

class TeamFeatureTest extends TestCase
{
    use TeamTestHelpers;
    
    public function test_user_can_create_team()
    {
        $user = User::factory()->create();
        $tenant = $this->createTenant();
        
        $this->actingAsTenant($tenant)
             ->actingAs($user);
        
        $team = $this->createTeam([
            'name' => 'Test Team',
            'owner_id' => $user->id,
        ]);
        
        $this->assertDatabaseHas('teams', [
            'name' => 'Test Team',
            'tenant_id' => $tenant->id,
        ]);
        
        $this->assertTrue($user->ownsTeam($team));
    }
}

use Litepie\Teams\Analytics\TeamAnalytics;

$analytics = TeamAnalytics::for($team)
    ->period('last_30_days')
    ->metrics(['activity', 'files', 'members'])
    ->get();

// Get insights
$insights = $team->getAnalytics([
    'member_activity' => true,
    'file_usage' => true,
    'collaboration_metrics' => true,
]);

use Litepie\Teams\Templates\TeamTemplate;

// Create team from template
$template = TeamTemplate::find('development_team');
$team = $template->createTeam([
    'name' => 'New Dev Team',
    'owner_id' => $user->id,
]);

// Templates 

use Litepie\Teams\Actions\BulkTeamOperationsAction;

// Bulk member operations
BulkTeamOperationsAction::execute($team, [
    'operation' => 'add_members',
    'users' => [$user1->id, $user2->id, $user3->id],
    'role' => 'member',
]);

// Bulk permission updates
BulkTeamOperationsAction::execute($team, [
    'operation' => 'update_permissions',
    'members' => [$user1->id, $user2->id],
    'permissions' => ['edit_content', 'manage_files'],
]);
bash
php artisan vendor:publish --tag="teams-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="teams-config"
bash
   cp .env.example .env
   php artisan key:generate
   php artisan migrate --env=testing