Skip to content

Instantly share code, notes, and snippets.

@GeniJaho
Created December 24, 2021 14:48
6 - Join team action
<?php
namespace App\Actions\Teams;
...
class JoinTeamAction
{
public function run(User $user, Team $team)
{
// Have the user join this team and restore their contributions
$userPhotosOnThisTeam = $user->photos()->whereTeamId($team->id);
$user->teams()->attach($team, [
'total_photos' => $userPhotosOnThisTeam->count(),
'total_litter' => $userPhotosOnThisTeam->sum('total_litter')
]);
$this->setAsActiveTeamIfNull($user, $team);
$team->members++;
$team->save();
}
protected function setAsActiveTeamIfNull(User $user, Team $team): void
{
if (is_null($user->active_team)) {
$user->active_team = $team->id;
$user->save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment