>我有一个如下所示的搜索表单,只有一个字段
{!! Form::open(array('method' => 'POST',
'action' => 'CustomerController@SearchCustomers',
'class' => "form-horizontal form-label-left")) !!}
{!! csrf_field() !!}
<input type="text" class="form-control" name="Customer">
<button type="submit">Search</button>
{!! Form::close() !!}
下面是控制器中的代码
$AllCustomers = 'App'Models'Customer_Model
::where('Customer', 'LIKE', '%'.$Customer.'%')
->get();
return View('Customer.List', array('AllCustomers' => $AllCustomers));
我在尝试什么?
当表单提交进行搜索时,在视图中,我应该能够在文本框中再次查看关键字。为此,我在下面做。
return View('Customer.List', array('AllCustomers' => $AllCustomers, 'Key' => $Customer));
现在在表格中,我在下面做。
<input type="text" class="form-control" name="Customer" value="{{$Key}}">
问题
有没有更好的方法来填写上面的搜索表单中的表单输入值?
我认为正确的方法是使用输入内联检查$request->input('key', null)
检查请求中是否存在密钥,如果是这样,它将返回null,否则返回密钥,检查下面的代码。
控制器:
public function index(Request $request)
{
$key = $request->input('key', null);
$allCustomers = 'App'Models'Customer_Model
::where('Customer', 'LIKE', '%'.$Customer.'%')->get();
return View('Customer.List', compact('allCustomers', 'key'));
}
视图:
<input type="text" class="form-control" name="Customer" value="{{is_null($key)?'':$key}}">
希望这有帮助。
我认为这可能是您正在寻找的机器人:
use Illuminate'Http'Request;
class MyController extends Controller
{
/**
* My action.
*
* @param Request $request
* @return View
*/
public function index(Request $request)
{
...
return View('Customer.List', array_merge(
['AllCustomers' => $AllCustomers],
$request->only(['Customer'])
));
}
操作方法中的use
行和类型提示参数执行 Request 对象的依赖项注入。
将$request->only(['Customer'])
合并到视图参数中可为您提供所需的"带输入"功能。
拉维尔文档:https://laravel.com/docs/master/requests