PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] Laravel redirect back with variable not working

 March 01, 2022     laravel-5.4, laravel-blade, php     No comments   

Issue

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"
                                  ]
          ]

Solution

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.



Answered By - Stem Florin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing