i am building a laravel aplication and i have this line of code which should redirect the user back to form he just submited , with the old input and the result of some operations .
return back()->with(["result" => round($area, 2)])->withInput($request->all());
The problem is that i only receive the old input in blade and the $result
variable is not available in the view.
This is how i try to output the result:
<input type="text" name="result" value="{{isset($result)&&old('roofType')==0?$result:''}} ㎡ " class="form-control input-sm" >
And here is what variables i have in the view after submit:
{{ dd(get_defined_vars()['__data']) }}:
array:7 [▼
"__env" => Factory {#89 ▶}
"app" => Application {#3 ▶}
"errors" => ViewErrorBag {#169 ▶}
"roofName" => "Acoperis intr-o apa"
"roofType" => "1"
"roofFolder" => "A1"
"baseFields" => array:3 [▼
0 => "L"
1 => "l"
2 => "H"
]
]
return redirect()->back()->with('result',round($area, 2))->withInput($request->all());
call {{Session::get('result')}} in your blade.
The problem was that I thought that writing return back()->with('bladeVar', $controllerVar)
was the same as return view('test')->with('bladeVar', $controllerVar);
,but it wasn't .
You cannot echo a variable using blade normal syntax: {{$bladeVar}}
, Instead, you have to access the session to get the value: {{session('bladeVar')}}
.
After I changed the way I displayed the data all worked as expected.
The answer is you can not.
If you want to use with()
then use it with view()
like:
return view('welcome')->with(['name' => 'test']);
You can not use with()
with back()
and redirect()
. It won't give you any error but you will not get the variable on the view.
More info: https://laravel.com/docs/master/views#passing-data-to-views