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

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

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>

图片转代码服务由CSDN问答提供 功能建议

我是Laravel的新手,我正在尝试从我从收到的控制器传递值 &lt; form&gt; 到视图并在文本框中显示相同内容。 虽然,我已经想出了如何使用方法链接做同样的事情,但我想使用数组传递值并在视图中的文本框中显示相同的内容。

我期待什么 要做什么?

controller 中,而不是方法链接:

返回视图('showvalues' ) - &gt; with(['name'=&gt; $ name,'address'=&gt; $ address]);

代码到目前为止,

controller

  public function showvalues(Request $ request)
 {
 $ name = $ request-&gt;  get('name'); 
 $ address = $ request-&gt; get('address'); 
 $ pass = $ request-&gt; get('password'); 
 $ arr = array(“$  name“,”$ address“,”$ pass“); 
 
返回视图('showvalues') - &gt; with('name',$ name) - &gt; with('address',$ address);  
 <
   
 
 

showvalues 视图中显示值:

 &lt; html&gt  ; 
&lt; head&gt; 
 
&lt; / head&gt; 
&lt; body&gt; 
&lt; h1&gt; 
显示值页面。
&  lt; / h1&gt; 
&lt; input type =“text”name =“n1”value =“&lt;?php echo $ name;?&gt;”  /&gt;&lt; br&gt; 
&lt; input type =“text”name =“n3”value =“&lt;?php echo $ address;?&gt;”  /&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题