请教大家,怎么在验证器中指定数据库中的数据表,去验证用户名唯一性?
项目里边有两个数据库,现在是在指定的数据库mysql_wx上重构,
下边代码里边验证会出错,有这个提示
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user ‘forge‘@’localhost’ (using password: NO) (SQL: select count(*) as aggregate from user
where username
= admin)
请怎么怎么在这个代码
Rule::unique(‘yt_user’, ‘username’) //用户名不能重复
里边指定数据库mysql_wx的数据表,谢谢!
<?php
namespace App\Http\Requests;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;
class YtuserRegister extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'username' => [
'required',
Rule::unique('user', 'username') //用户名不能重复
],
'password' => 'required|same:password2', //密码不能为空,要相同
'nickname' => 'required', //昵称不能为空
];
return $rules;
}
//框架不知道的字段名增加上去 对应的中文
public function attributes()
{
return [
'password2' => '确认密码',
'nickname' => '昵称',
];
}
}
推荐文章: