where('voter_id', $profile->getKey()) ->get(); // A user's own votes on behalf of an organization / bank / admin in "one vote per linked user" polls. // In "one vote per profile" polls the vote belongs to that profile and stays. if ($profile instanceof User) { $responses = $responses->merge( PollResponse::where('user_id', $profile->getKey()) ->where('voter_type', '!=', User::class) ->whereHas('poll', fn ($query) => $query->where('vote_scope', Poll::VOTE_SCOPE_PER_USER)) ->get() ); } foreach ($responses as $response) { DB::transaction(function () use ($response) { if ($response->ledger_sequence) { $this->ledger->append( $response->poll, PollLedgerEntry::WITHDRAW, ['reason' => 'profile_deleted'], null, $response->ledger_sequence ); } $response->delete(); // answers cascade }); } return $responses->count(); } }