/** * Give a completed Badge for an User. * * @param User $user * @param Badge $badge * * @return bool */ public static function addBadge(User $user, Badge $badge) { if ($user->hasBadgeCompleted($badge)) { return true; } $data = ['amount' => $badge->amount_needed, 'completed' => true, 'completed_on' => Carbon::now()]; if ($userBadge = $user->badges()->find($badge->id)) { // this badge was initiated before $saved = $userBadge->pivot->save($data); } else { // this is the first occurrence of this badge for this user $user->badges()->attach($badge->id, $data); $saved = true; } return $saved; }