deletesCompanies = $deletesCompanies; } /** * Delete the given user. */ public function delete(User $user): void { DB::transaction(function () use ($user) { $this->deleteCompanies($user); $user->deleteProfilePhoto(); $user->connectedAccounts->each(static fn ($account) => $account->delete()); $user->tokens->each(static fn ($token) => $token->delete()); $user->delete(); }); } /** * Delete the companies and company associations attached to the user. */ protected function deleteCompanies(User $user): void { $user->companies()->detach(); $user->ownedCompanies->each(function (Company $company) { $this->deletesCompanies->delete($company); }); } }