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?
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 replaceby