Laravel JavaScript将数组从后端传递到JavaScript中的数组 [英] Laravel JavaScript passing an array from backend to a array in JavaScript

查看:578
本文介绍了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

这篇关于Laravel JavaScript将数组从后端传递到JavaScript中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆