解决 Laravel 无法检测到“用户名”输入的问题
<p>我是laravel的新手,我的代码之前是可以工作的,但是我把"name"改成了"username",然后如果我点击提交,它会说它没有默认值。</p>
<pre class="brush:php;toolbar:false;">class UserSignup extends Controller {
public function register(Request $request) {
$validatedData = $request->validate([
'username' => ['required', 'string','max:255', Rule::unique('users')],
'email' => ['required', 'email', 'max:255', Rule::unique('users')],
'password' => 'required|min:8'
]);
$validatedData['password'] = bcrypt($validatedData['password']);
$user = User::create($validatedData);
return response()->json(['message' => 'User created successfully!', 'user' => $user]);
}
}</pre>
<p>我尝试将"name"改成"username"如下所示</p>
<pre class="brush:php;toolbar:false;">$validatedData = $request->validate([
'username' => ['required', 'string','max:255', Rule::unique('users')],
'email' => ['required', 'email', 'max:255', Rule::unique('users')],
'password' => 'required|min:8'
]);</pre>
<p>点击提交后会出现一个错误,说它没有默认值。</p>
<p>我只想把'username'放入数据库中。</p>