doz22551 2015-10-31 16:10
浏览 518
已采纳

从laravel控制器获取php变量到javascript

Im trying to send a variable from a controller to a JavaScript, I have my controller, and I can send an array:

public function index()
{

    $casa = new Casa(true);

    $result = $casa->where(['show'=>true])->get(); 
    return view('casa', array('casa' => $result));
}

If i go to my HTML and I make and echo:

<html>
 <body>
  <?php echo $casa ?>
 </body> 
</html>

I can show my array in the body, I was thinking about make an invisible element and get the array with document.getElementById().innerHTML, but I think this is not the best way.

Also, I think that I could make an Ajax petition sending a post and get the result, but I dont know if I can get my variable in a simpler way.

I tried to make and echo in Javascript and doesn´t work. some ideas?

Can I have 2 method post to get request in my controller? I already have one to get data from a form, and if I set the Ajax request I will have two post request. I would like have just one.

  • 写回答

2条回答 默认 最新

  • doupeng8419 2015-10-31 18:50
    关注

    Jeffrey Way created a package for that. He also made a video explaining it in laracasts.com. The package can be found here: https://github.com/laracasts/PHP-Vars-To-Js-Transformer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序