Laravel 5.1使用变量重定向 [英] Laravel 5.1 Redirect with variable
问题描述
我正在尝试像这样在Laravel中重定向:
I am trying to redirect in Laravel like this:
在我的控制器中:
$state = false;
return redirect()->route('newPr')->with('errorMessageDuration', 'error', $state);
在我看来,我试图像这样获得它:
and in my view I try to get it like this:
<input id="PrId" type="text" value="{{ isset($state) ? $state : '' }}">
但由于某种原因它不起作用,它一直为空.
but somehow it does not work, it keeps being empty.
我希望将其永久保存.
推荐答案
将withErrors()
与关联数组一起使用,就像这样:
Use withErrors()
with an associative array instead, like so:
return redirect()->route('newPr')->withErrors(compact('state'));
然后,您只需在视图中使用$ errors变量即可.
Then you can simply use the $errors variable in your view.
来自文档:
始终会定义$ errors变量,并且可以安全地使用它.这 $ errors变量将是Illuminate \ Support \ MessageBag的一个实例. 有关使用此对象的更多信息,请查看其对象. 文档.
The $errors variable is always defined and can be safely used. The $errors variable will be an instance of Illuminate\Support\MessageBag. For more information on working with this object, check out its documentation.
这篇关于Laravel 5.1使用变量重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!