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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?