You can use the @json directive:
x-data="{ feed: @json([1,2,3,4,5]) }"
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a PHP array $authors->books and I want to pass into this x-data instead of the [1,2,3,4,5], how do I do that?
x-data="{ feed: [1,2,3,4,5] }"
@SarahS74 you have an associative array for $authors->books;
What you want is an array of the keys only???
x-data="{ feed: @json(array_keys($authors->books))}"
or the values only???
x-data="{ feed: @json(array_values($authors->books))}"
Please or to participate in this conversation.