duandie5707 2018-04-27 06:59
浏览 612
已采纳

如何将数组中的值从控制器传递到视图

I m new to Laravel and I am trying to pass values from a controller which I have received from a <form> to a view and display the same in text boxes. Although, I have figured out how to do the same using method chaining but I would like to pass the values using an array and show the same into the textboxes in the view.

What I expect to do?

In the controller, instead of method chaining:

return view('showvalues')->with(['name'=>$name, 'address'=>$address]);

Code so far,

controller

 public function showvalues(Request $request)
    {
        $name=$request->get('name');
        $address=$request->get('address');
        $pass=$request->get('password');
        $arr=array("$name","$address","$pass");

        return view('showvalues')->with('name',$name)->with('address',$address);
    }

Show values in showvalues view:

<html>
    <head>

    </head>
    <body>
        <h1>
            Show Value Page.
        </h1>
        <input type="text" name="n1" value="<?php echo $name;?>" /><br>
        <input type="text" name="n3" value="<?php echo $address;?>" />
    </body>
</html>
  • 写回答

4条回答 默认 最新

  • douchilian1009 2018-04-27 07:01
    关注

    Use the compact method as the second argument to view:

    public function showvalues(Request $request)
    {
        $name = $request->get('name');
        $address = $request->get('address');
        $pass = $request->get('password');
    
        return view('showvalues', compact('name', 'address', 'pass'));
    }
    

    The variables will be available in your view file by the same name, you can display them like:

    {{ $name }}
    {{ $address }}
    {{ $pass }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?