I want to pass/store Laravel array in JavaScript variable. I used ->all()
so I get the result like this rather than object:
array:83 [▼ 0 => 1 1 => 11 2 => 12 ... ]
I can access this in view using {{ $theArray }}
.
However whatever I tried, I couldn't make this to javascript array.
I tried
var array = {{ $theArray }};
var array = {{{ $theArray }}};
I feel like I'm close but I couldn't figure it out
You can easily use PHP array in javascript you only need to convert PHP array into JSON format Using json_encode() function. PHP array can be converted to JavScript array and accessible in JavaScript. Whatever the array type is, a single or multidimensional or indexed or associative array.
PHP array can be used in JavaScript, whatever the array is a single or multidimensional or indexed or associative array. You can convert PHP array to JavaScript array easily with a single line of code. Using json_encode() function, PHP array can be converted to JavScript array and accessible in JavaScript.
var app = @json($array);
Works like a charm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With