How can I do to view all items in the twig?

477 views Asked by At

I am trying to display the information like this:

Controller:

public function index(RestaurantRepository $restaurantRepository)
{
    $restaurant = $restaurantRepository->findAll();

    return $this->render('restaurant/index.html.twig', [
        'restaurant' => $restaurant
    ]);
}

twig:

{% for restaurants in restaurant %}
    {{ restaurant.name }}
{% endfor %}

Error "Key" name "for array with keys" 0, 1, 2, ..... "does not exist. But if I type in twig restaurant [0] .name etc it will show me the name in the given index.

What am I doing wrong?

1

There are 1 answers

0
doydoy44 On

It is a problem of plurals.

In your controller, restaurant is a collection, you should replace $restaurant by $restaurants and 'restaurant' => $restaurant by 'restaurants' => $restaurants

In twig the problem is here {{ restaurant.name }} instead {{ restaurants.name }} So you should replace

{% for restaurants in restaurant %}
    {{ restaurant.name }}
{% endfor %}

by

{% for restaurant in restaurants %}
    {{ restaurant.name }}
{% endfor %}