Laravel JavaScript将数组从后端传递到JavaScript中的数组
我有一个控制器,该控制器将数组传递给刀片文件,称为 $ data 。从那里,我像 {{$ data [0] ['名称]}}
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] }}
问题是我也希望数据位于javascript数组中。
The problem is I also want the data to be inside an array in javascript.
这如何完成?
最好只是通过AJAX再次请求数据,或者是否有办法将数据从刀片中获取并放入javascript中。或者可以通过Blade将其呈现为JSON中的html,然后从JavaScript中的html中提取JSON
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
因此,只需将数据以与php相同的方式传递给javascript:< script> var a = ['{{$ data [0] ['name]}}','{{$ data [0] ['name]}}'];< / script>
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>
或者,如果您不想遍历每个人并手动添加它们,请使用内置于foreach循环中的laravel:
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