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

问题描述:

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>

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

我期待什么 要做什么? h3>

controller code>中,而不是方法链接: p>

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

代码到目前为止, h3>

controller code> p>

  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);  
   pre> 
 
 

showvalues code>视图中显示值: p>

 &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; 
  code>  pre> 
  div>

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 }}

Pass array as second argument to view():

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

Btw, did you try to open Laravel's manual?

You need to return all form inputs with compact function.

Controller

public function showvalues(Request $request)
{
    $form = $request->all();

    return view('showvalues', compact('form'));
}

View

{{ $form['name'] }}
{{ $form['address'] }}

what about

return view('showvalues')->withName($name);

Or

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