A
A
Alexey Orlov2020-02-27 19:41:11
Twig
Alexey Orlov, 2020-02-27 19:41:11

How to count the number of positive comments?

Hello! There is a list of testimonials that may or may not contain positive comments. Question: How to correctly display the number of reviews that contain positive comments?

{% for item in ReviewComponent.reviews.items %}
    {% if item.attributes.page_casino == record.slug %}
        {% component 'ReviewComponent' %}
        {{ dump(item.positive)}} - положительный комментарий
    {% endif %}
{% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Popov, 2020-02-27
@be_a_dancer

It is advisable to move the logic of positive comments to the controller / service, and not to do it in the view.
I would do it something like this:

public function getPositive(array $reviews): integer 
{
    return collect($reviews)->filter(function ($el) {
        retrun $el->rating > 2; // Здесь логика фильтрации.
    })->count();
}

Somewhere inside your controller, call this function and give the finished figure to the view.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question