DEMO / LiveComponent

Up & Down Voting

With each row as its own component, it's easy to add up & down voting + keep track of which items have been voted on.
This uses a LiveAction to save everything with Ajax.

Banana 🍌 Votes: 64
Apple 🍎 Votes: 79
Hamburger 🍔 Votes: 15
Watermelon 🍉 Votes: 43
Cheese 🧀 Votes: 63
Pizza 🍕 Votes: 11
Pretzel 🥨 Votes: 51
Donut 🍩 Votes: 89
Pineapple 🍍 Votes: 34
Popcorn 🍿 Votes: 64
Egg 🍳 Votes: 54
Taco 🌮 Votes: 33
Ice Cream 🍦 Votes: 35
Cookie 🍪 Votes: 23
// ... use statements hidden - click to show
use App\Entity\Food;
use App\Repository\FoodRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class FoodVote extends AbstractController
{
    use DefaultActionTrait;

    #[LiveProp]
    public Food $food;

    #[LiveProp]
    public bool $hasVoted = false;

    public function __construct(private FoodRepository $foodRepository)
    {
    }

    #[LiveAction]
    public function vote(#[LiveArg] string $direction)
    {
        if ('up' === $direction) {
            $this->food->upVote();
        } else {
            $this->food->downVote();
        }

        $this->foodRepository->add($this->food, true);
        $this->hasVoted = true;
    }
}
<tr {{ attributes }}>
    <th>{{ food.name }}</th>
    <td>
        Votes: {{ food.votes }}
    </td>
    <td style="width: 250px;">
        {% if hasVoted %}
            <div class="alert alert-success">
                Thanks for voting! <twig:Icon name="circle-check" />
            </div>
        {% else %}
            <button
                type="button"
                class="btn btn-secondary"
                data-action="live#action"
                data-live-action-param="vote"
                data-live-direction-param="up"
            >
                <twig:Icon name="arrow-up" />
            </button>
            <button
                type="button"
                class="btn btn-secondary"
                data-action="live#action"
                data-live-action-param="vote"
                data-live-direction-param="down"
            >
                <twig:Icon name="arrow-down" />
            </button>
        {% endif %}
    </td>
</tr>