我正在尝试创建一个搜索表单,用户必须从其中的一些下拉菜单中进行选择,然后在几个字段之一中输入文本。问题是我正在重新显示搜索页面,搜索结果位于其下方。为此,我没有重定向,我只是返回了一个视图,其中包含需要压缩的数据集。
验证失败后进行重定向时,有什么方法可以像在Input :: old('x')中那样检索输入?
路线是:
Route::get('search', ['as' => 'main.search.get', 'uses' => 'MainController@showSearchPage']);
Route::post('search', ['as' => 'main.search.post', 'uses' => 'MainController@showSearchResults']);
视图中的代码示例:
{!! Form::open(array('route' => 'main.search.post', 'class' => 'form-inline align-form-center', 'role' => 'form')) !!}
<div class="form-group">
{!! Form::label('product_code', 'Product Code: ',['class' => 'control-label label-top']) !!}
{!! Form::text('product_code', Input::old('product_code'), ['class' => 'form-control input-sm']) !!}
</div>
因此,当您提交搜索时,它将调用showSearchResults,如果成功,它将返回一个视图;如果通过我的SearchRequest类进行的验证失败,它将被重定向到main.search.get路由,输出错误并将输入返回到领域。
我做了很多搜索,或多或少地空手而归,如果在返回视图(非重定向)之类的时候有一种说法-> withInput()的话,那将是很好的。
目前,我唯一的解决方案是使用Input :: flash(),但由于我没有重定向,因此该数据会保留下来以进行额外的刷新。目前,这还不是什么大不了的事,但是我想知道是否还有其他人有更好的解决方案。
编辑-下面是返回视图的控制器的代码:
...
Input::flash();
return view('main.search', compact('results', 'platformList', 'versionList', 'customerList', 'currencyList', 'customer', 'currency'));
}
谢谢
我有同样的问题。对我有用的解决方案是将以下行添加到控制器中。
session(['_old_input' => $request->input()]);
现在我将解释它的工作原理。
在视图中,全局函数old()称为:
<input type="username" id="username" class="form-control" name="username" value="{{ old('username') }}" placeholder="Username" autofocus>
此功能位于vendor / laravel / framework / src / Illuminate / Foundation / helpers.php
function old($key = null, $default = null)
{
return app('request')->old($key, $default);
}
此调用Illuminate \ Http \ Request-> old():
public function old($key = null, $default = null)
{
return $this->session()->getOldInput($key, $default);
}
哪个调用Illuminate \ Session \ Store-> getOldInput():
public function getOldInput($key = null, $default = null)
{
$input = $this->get('_old_input', []);
return Arr::get($input, $key, $default);
}
此呼叫正在会话中寻找_ old_input。因此解决方案是使用此值将输入添加到会话中。
希望这会有所帮助。
您可以使用request
代替old
,因为它是发布请求
change {{old('product_code')}} to {{request('product_code')}}