1

This is my RegisterController (Laravel ui edited). please help me. Is i have to add any other page to this question.

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'name_of_firm' => $data['name_of_firm'],
        'number' => $data['number'],
        'address' => $data['address'],
    ]);
}

enter image description here

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24

2 Answers2

1

There is 2 case if name_of_firm is nullable or required

Case : 1

if name_of_firm is required

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'name_of_firm' => ['required']
    ]);
}


/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'name_of_firm' => $data['name_of_firm'],
        'number' => $data['number'],
        'address' => $data['address'],
    ]);
}

Case : 2

if name_of_firm is nullable

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}


/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'name_of_firm' => $data['name_of_firm'] ?? null, // make sure database is accept null value
        'number' => $data['number'],
        'address' => $data['address'],
    ]);
}
Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • And what can we do in case we have a field that can be, for example, null or a foreign key ID? How can we add that kind of validation logic? – Aridez Sep 27 '22 at 12:38
  • if field accept null you can use case 2 – Kamlesh Paul Sep 28 '22 at 04:05
  • Not exactly the same since I would need "name_of_firm" to have some rules and the error would still be thrown. In case someone finds themselves with the same case, using the "sometimes" rule in addition to the "nullable" rule solves this and allows other rules when the field is not null and present in the request. – Aridez Sep 28 '22 at 09:31
0

you should use data_get or Arr::get() helpers to solved not exists element:

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'name_of_firm' => data_get($data, 'name_of_firm'),
        'number' => data_get($data, 'number'),
        'address' => data_get($data, 'address')],
    ]);
}
Thai Nguyen Hung
  • 1,154
  • 5
  • 13