如何使用两个值的总和重定向我的表单并将其显示在Laravel的只读输入字段中?
I am making a simple form two sum two values. I want to display the calculated result in an readonly input after the request went through. If I dd() the sum of my two fields, the sum is correct. But I am unable to pass the sum back to the view.
I searched around the web and found nothing promising. Same with the Laravel documentation.
Form:
<p>
<input type="number" name="numberOne" value="{{ old('numberOne') }}">
</p>
<p>
<input type="number" name="numberTwo" value="{{ old('numberTwo')}}">
</p>
<p>
<input type="number" name="calculated" value="{{ isset($calculated) ? $calculated : '' }}" readonly>
</p>
Controller:
class CalculationsController extends Controller
{
public function process(Request $request) {
$numberOne = $request->input('numberOne');
$numberTwo = $request->input('numberTwo');
$calculated = $numberOne + $numberTwo;
dd($calculated);
return redirect('/')->withInput();
}
}
Expected would be the sum of both inputs in the readonly. But after the submit, it's still empty. What am I doing wrong?
我正在制作一个两个和两个值的简单形式。 我想在请求通过后在readonly输入中显示计算结果。 如果我dd()我的两个字段的总和,总和是正确的。 但我无法将这笔款项转回视图。 p>
我在网上搜索,没有发现任何有希望的事情。 与Laravel文档相同。 p>
表单: p>
&lt; p&gt;
&lt; input type =“number”name =“numberOne “value =”{{old('numberOne')}}“&gt;
&lt; / p&gt;
&lt; p&gt;
&lt; input type =”number“name =”numberTwo“value =”{{old(' numberTwo')}}“&gt;
&lt; / p&gt;
&lt; p&gt;
&lt; input type =”number“name =”calculated“value =”{{isset($ calculated)?$ calculated:''} }“readonly&gt;
&lt; / p&gt;
code> pre>
控制器: p>
类CalculationsController extends Controller
{
公共功能流程(请求$请求){
$ numberOne = $ request-&gt; input('numberOne');
$ numberTwo = $ request-&gt; input('numberTwo');
$ calculated = $ numberOne + $ numberTwo;
dd($ calculated);
返回redirect('/') - &gt; withInput();
}
}
code> pre>
预期将是readonly中两个输入的总和。 但提交后,它仍然是空的。 我做错了什么? p>
div>
When you use the redirect()
function the variable $calculated
isn't passed to the view. I think the best solution is passing it by Session. Your redirect must be something like that:
return redirect('/')->withInput()->with('calculated', $calculated);
The with()
function is used to pass data through the session. To retrieve it in your view, do:
<input type="number" name="calculated" value="{{ session()->has('calculated') ? session('calculated') : '' }}" readonly>
For documentation, check: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data https://laravel.com/docs/5.7/session#retrieving-data
$request->merge(['calculated' => $caluclated]);
return redirect('/')->withInput($request->all());
<input type="number" name="calculated" value="{{ old('calculated') }}" readonly>
Add calculated value before redirect,
class CalculationsController extends Controller
{
public function process(Request $request) {
$numberOne = $request->input('numberOne');
$numberTwo = $request->input('numberTwo');
$calculated = $numberOne + $numberTwo;
$request->merge(['calculated' => $calculated ]);
// dd($calculated);
return redirect('/')->withInput();
}
}