Laravel 5 制作自定义注册函数
Laravel 5 make a custom register function
Laravel 5 有它的默认寄存器功能,它在
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
我知道我可以复制此代码并将其粘贴到我的 AuthController
中,但我需要进行更改,但我不知道从哪里开始和查找。我想要的是更改代码以在我的 users
table 中插入数据。我想更改此设置,因为我在 users
table 中添加了另一列,即 company_name
并且我有一个 table ,它被命名为 companies
所以基本上当用户输入 company_name
进行注册,它将检查 companies
table 是否存在,如果存在则检查 return 错误消息。所以想想有这样的东西:
$rules = array(
'company_name' => 'unqiue:companies',
);
但是我不知道把这个东西放在我的注册码的什么地方。谢谢
在这种情况下,您可以使用自定义验证。 确保您调用的是 $this->validate()
,而不是 $this->validator
。 如果失败,此验证将自动重定向并返回错误,因此您可以跳过检查语句.
public function postRegister(Request $request)
{
$this->validate($request->all(), [
'company_name' => 'unique:companies',
// And the other rules, like email unqiue, etc..
]);
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
Laravel 5 有它的默认寄存器功能,它在
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
我知道我可以复制此代码并将其粘贴到我的 AuthController
中,但我需要进行更改,但我不知道从哪里开始和查找。我想要的是更改代码以在我的 users
table 中插入数据。我想更改此设置,因为我在 users
table 中添加了另一列,即 company_name
并且我有一个 table ,它被命名为 companies
所以基本上当用户输入 company_name
进行注册,它将检查 companies
table 是否存在,如果存在则检查 return 错误消息。所以想想有这样的东西:
$rules = array(
'company_name' => 'unqiue:companies',
);
但是我不知道把这个东西放在我的注册码的什么地方。谢谢
在这种情况下,您可以使用自定义验证。 确保您调用的是 $this->validate()
,而不是 $this->validator
。 如果失败,此验证将自动重定向并返回错误,因此您可以跳过检查语句.
public function postRegister(Request $request)
{
$this->validate($request->all(), [
'company_name' => 'unique:companies',
// And the other rules, like email unqiue, etc..
]);
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}