Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output array in Twig

I trying to output an array from the database to the screen. In my entity:

/**
 * @ORM\Column(type="array", nullable=true)
 */
private $category;

In my twig template:

{% for category in user.profile.category %}
    {{ category }}
{% endfor %}

Error: Array to string conversion in ...

Where is my mistake?

like image 212
nowiko Avatar asked Oct 13 '14 12:10

nowiko


Video Answer


1 Answers

So, as error shows you are trying convert array (in category variable) to string. You can preview array by dump() (doc.). In your case:

{% for category in user.profile.category %}
    {{ dump(category) }}
{% endfor %}

Please notice that dump() should be use only for debugging.

like image 181
NHG Avatar answered Oct 13 '22 00:10

NHG