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.
In the controller
, instead of method chaining:
return view('showvalues')->with(['name'=>$name, 'address'=>$address]);
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>
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]);