[英]Laravel request->input('') or old('') always return 1 in blade
I want to get the url data into my input value.我想将 url 数据放入我的输入值中。
Example:示例:
- the url is http://example.com/user?username=example网址是http://example.com/user?username=example
- and my html input code is as below我的html输入代码如下
<input value="{{ request()->input('username') or old('username') }}">
- but the result always return "1" and not "example"但结果总是返回“1”而不是“示例”
It seems like the "or" blade helper acts as conditional operator.似乎“或”刀片助手充当条件运算符。
How can I get the username value from url into that input and still get the default value as old('username')?如何从 url 获取用户名值到该输入中并仍然获得默认值作为 old('username')?
Try with试试
<input value="{{ request()->input('username', old('username')) }}">
It will retrieve username as an input if it exists or default to the old value.如果用户名存在或默认为旧值,它将检索用户名作为输入。 You can also give a default value to the old helper.您还可以为旧助手提供默认值。
这种方式更简单:
<input value="{{ (old('username')?old('username'):'') }}">
很简单的方法。
<input value="{{ request()->input('username') ?? old('username') }}">
As per official documentation 8.x根据官方文档8.x
We use the helper request
我们使用帮助request
The request function returns the current request instance or obtains an input field's value from the current request: request 函数返回当前请求实例或从当前请求中获取输入字段的值:
$request = request();
$value = request('key', $default);
the value of request is an array you can simply retrieve your input using the input key as follow request 的值是一个数组,您可以使用输入键简单地检索您的输入,如下所示
$username = request()->username; //for http://example.com/user?username=example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.