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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?