Search code examples
laravelformsregistrationuser-registration

Laravel register form with 2 fields with same name saved to different tables


I have a custom user registration form, it registers a new user and a new church team at the same time. So this saves data to 2 different tables, the user table and the team table. This form includes an input field for the User's phone number as well as the church Team's phone number. The issue occurring is that the phone number entered into the second phone field is overriding the first and saving into both tables rather than having them both saved respectively.

This is the phone input field from the registration form, this is the first one, it receives the Users phone number. The second field, which receives the Church teams phone number is identical except for the language which is @lang('global.teams.fields.phone')

<div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
     <label for="phone" class="col-md-4 control-label">@lang('global.app_phone')</label>

       <div class="col-md-6">
           <input id="phone" type="text" class="form-control" name="phone" value="{{ old('phone') }}" >

           @if ($errors->has('phone'))
              <span class="help-block">
              <strong>{{ $errors->first('phone') }}</strong>
              </span>
           @endif
        </div>
</div>

Here is my Controller Validator and create code:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name'                  => 'required|max:191',
            'email'                 => 'required|email|max:191|unique:users',
            'password'              => 'required|min:6|confirmed',
            'church_name'           => 'required|max:191',
            'address'               => 'required|max:191',
            'state'                 => 'required|max:191',
            'city'                  => 'required|max:191',
            'zip'                   => 'required|max:191',
            'phone'                 => 'required|max:191',
            'g-recaptcha-response'  => [new ReCaptcha],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     * @return User
     */
    protected function create(array $data)
    {

        $team = Team::create([
            'name' => $data['church_name'],
            'address'  => $data['address'],
            'city'  => $data['city'],
            'state' => $data['state'],
            'zip' => $data['zip'],
            'phone' => $data['phone'],

        ]);


        $user = User::create([
            'name'     => $data['name'],
            'email'    => $data['email'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
            'team_id' => $team->id

        ]);

        $user->role()->attach(config('app_service.default_role_id'));

        return $user;

I would like to save the two different unique phone numbers into their respective tables, what needs to occur in this code to get that result?


Solution

  • This should work:

    <div class="form-group{{ $errors->has('user_phone') ? ' has-error' : '' }}">
         <label for="user_phone" class="col-md-4 control-label">@lang('global.app_user_phone')</label>
    
           <div class="col-md-6">
               <input id="user_phone" type="text" class="form-control" name="user_phone" value="{{ old('user_phone') }}" >
    
               @if ($errors->has('user_phone'))
                  <span class="help-block">
                  <strong>{{ $errors->first('user_phone') }}</strong>
                  </span>
               @endif
            </div>
    </div>
    
    <div class="form-group{{ $errors->has('church_team_phone') ? ' has-error' : '' }}">
         <label for="church_team_phone" class="col-md-4 control-label">@lang('global.app_church_team_phone')</label>
    
    <div class="col-md-6">
               <input id="church_team_phone" type="text" class="form-control" name="church_team_phone" value="{{ old('church_team_phone') }}" >
    
               @if ($errors->has('church_team_phone'))
                  <span class="help-block">
                  <strong>{{ $errors->first('church_team_phone') }}</strong>
                  </span>
               @endif
            </div>
    </div>
    

    and controller:

    protected function validator(array $data)
        {
            return Validator::make($data, [
                'name'                  => 'required|max:191',
                'email'                 => 'required|email|max:191|unique:users',
                'password'              => 'required|min:6|confirmed',
                'church_name'           => 'required|max:191',
                'address'               => 'required|max:191',
                'state'                 => 'required|max:191',
                'city'                  => 'required|max:191',
                'zip'                   => 'required|max:191',
                'user_phone'            => 'required|max:191',
                'church_team_phone'     => 'required|max:191',
                'g-recaptcha-response'  => [new ReCaptcha],
            ]);
        }
    
        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array $data
         * @return User
         */
        protected function create(array $data)
        {
    
            $team = Team::create([
                'name' => $data['church_name'],
                'address'  => $data['address'],
                'city'  => $data['city'],
                'state' => $data['state'],
                'zip' => $data['zip'],
                'phone' => $data['church_team_phone'],
    
            ]);
    
    
            $user = User::create([
                'name'     => $data['name'],
                'email'    => $data['email'],
                'phone' => $data['user_phone'],
                'password' => bcrypt($data['password']),
                'team_id' => $team->id
    
            ]);
    
            $user->role()->attach(config('app_service.default_role_id'));
    
            return $user;