Laravel JavaScript Passing An Array From Backend To A Array In JavaScript

- 1 answer

I have a controller that passes a array to the blade file call it $data From there I echo the data to the page via blade like {{ $data[0]['name] }}

The problem is I also want the data to be inside an array in javascript.

How can this be accomplished? Is it best to just request the data again via AJAX or is there a way to get the data out of blade and into javascript. Or possible rendering it to html in JSON via Blade then pulling the JSON from the html in JavaScript



So in doing this, you'd simply pass the data to javascript the same way you would with php: <script> var a = ['{{$data[0]['name] }}','{{$data[0]['name] }}'];</script>

Or, if you don't want to go through each individual one and add them by hand, use laravels built in foreach loop:

var a = [@foreach($data as $k => $info)
   '{{ $info }}',
@endforeach ]

This just depends on exactly how you plan on going about this