jackhammer013 jackhammer013 - 4 months ago 7
PHP Question

Laravel 5 make a custom register function

Laravel 5 has it's default register function which is in

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());
}


I know that I can copy this code and paste it in my
AuthController
but there's a change that I need to make which I don't know where to start and find. What I want is change the code for the insertion of data in my
users
table. I want to change this because I add another column in my
users
table which is
company_name
and I have a table which is named
companies
so basically when the user enter a
company_name
for registration it will check the
companies
table if it is existing then return error message if it is. So think there is something like:

$rules = array(
'company_name' => 'unqiue:companies',

);


But i don't know where to put this thing in my registration code. Thanks

Answer

You can use a custom validation, in this case. Make sure, you a are calling $this->validate(), and not $this->validator. This validate will automatically redirect back with errors if it fails, so you can skip the check statement.

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());
}