Laravel request->input('') 或 old('') 总是 return 1 in blade

Laravel request->input('') or old('') always return 1 in blade

我想将 url 数据放入我的输入值中。

Example:

  • the url is http://example.com/user?username=example
  • and my html input code is as below
  • <input value="{{ request()->input('username') or old('username') }}">
  • but the result always return "1" and not "example"

似乎 "or" blade 助手充当条件运算符。

如何将 url 中的用户名值输入到该输入中,并仍然获得旧值 ('username') 的默认值?

试试

<input value="{{ request()->input('username', old('username')) }}">

如果用户名存在或默认为旧值,它将检索用户名作为输入。也可以给老帮手一个默认值。

这样更简单:

<input value="{{ (old('username')?old('username'):'') }}">

非常简单的方法。

<input value="{{ request()->input('username') ?? old('username') }}">

根据official documentation 8.x

我们使用助手request

The request function returns the current request instance or obtains an input field's value from the current 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

$username = request()->username; //for http://example.com/user?username=example