Laravel redirect back with variable not working
Asked Answered
S

4

8

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:''}} &#x33a1; " 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"
                                  ]
          ]
Slipperwort answered 22/8, 2017 at 10:3 Comment(0)
S
21

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.

Slipperwort answered 22/8, 2017 at 11:41 Comment(2)
return back()->with('something') should be accessed by session only.Fixing
Spent a whole precious hour on the crazy stuff until found this. Thank you so much for sharing!Ilianailine
J
2

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

Jot answered 22/8, 2017 at 11:43 Comment(3)
i got the variable in my view , but not the usual way , i got it via session.Slipperwort
if I use the return view('welcome') the withImput() is not working anymore. Any idea how I can fix this then?Preview
What error are you getting? And please add code into question.Jot
D
1
return redirect()->back()->with('result',round($area, 2))->withInput($request->all());

call {{Session::get('result')}} in your blade.

Debar answered 22/8, 2017 at 10:16 Comment(2)
unfortunately i already tried this solution, and i also tired it again just now , and its not solving my problem ...Slipperwort
So, If directly not working u can use flash session which is as like as with() $request->session()->flash('result', round($area, 2));Debar
I
0
return view('profile.reset', compact('user'));
Implement answered 9/12, 2021 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.