在控制器中
public function store(Request $req)
{
$req->validate([
'username' => 'required|unique:users',
]);
$users = new Users();
$users->username = $req->username;
$users->save();
return redirect()->route('list.users');
}
用户名验证返回为The username has already been taken.
我将用户名字段键入为JHON,我将返回验证,例如:The JHON has already been taken.
?
答案 0 :(得分:1)
我相信您可以在定义自定义验证消息时使用:input
。
public function store(Request $req)
{
Validator::make($req, [
'username' => 'required|unique:users',
], [
'unique' => 'The :input username has already been taken.',
]);
$users = new Users();
$users->username = $req->username;
$users->save();
return redirect()->route('list.users');
}
答案 1 :(得分:1)
要使代码更整洁,应更改“唯一”验证错误的本地化
转到文件“ resources / lang / en / validation.php”(或使用您的语言的iso代码更改“ en”部分)并更改“唯一”值。
发件人:
'unique' => 'The :attribute has already been taken.',
收件人:
'unique' => 'The :attribute :input has already been taken.',
这样,您应该会收到以下消息
用户名JHON已被占用。
答案 2 :(得分:0)
$req->validate([
'username' => 'required|unique:users,username',
]);
这是使用唯一性的语法:
unique:table,column