Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar
Level 5

Query Builder: I want to add up the votes of the same person

Here is my SQL query:

$votes = DB::table('options')
	->select('id', 'poll_id', 'option_text', 'votes', 'created_at', 'updated_at')
	->get();

I want to add up the votes of the same person (for example "Le Pen")

"Le pen" is an option_text.

See the screencapture hereafter for better understanding: https://i.gyazo.com/583a151077c5a725381a81443f362bf5.png

For example Le Pen is counting 2 votes and another Le Pen is counting 1 vote.

The chart should display 3 votes (for Le Pen) and not 1 vote and 2 votes.

Thanks in advance, ilex01

0 likes
1 reply
ilex01's avatar
ilex01
OP
Best Answer
Level 5

Answer:

        $votes = DB::table('options')
            ->select('option_text', DB::raw('SUM(votes) as votes'))
            ->groupBy('option_text')
            ->get();
1 like

Please or to participate in this conversation.