title: Laravel 4 how to submit form data to different tables
body: <p>I have an edit form where the majority of the form's data will be posted to the users table. 
  For users that require a license their information will be posted to a license table, the license fields are only visible to the appropriate user type. I.E if the user type is a receptionist then they won't see the license fields but if the user type is a dentist then the fields will show etc. I have a user_id inside the license table so that I can associate the users with licenses. I set up a new eloquent model called license the majority of my work is being done in the update controller.........</p>
  
  <p><strong>License Model</strong></p>
  
  <pre><code>class License extends Eloquent {
  
  
      protected  $table = 'temps_license';
  
      public function user()
      {
          return $this-&gt;belongsTo('User');
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function update($id)
  {
  
      // validate
      // read more on validation at http://laravel.com/docs/validation
      $rules = array(
          'firstname' =&gt; 'required|min:3|alpha',
          'lastname' =&gt; 'required|min:3|alpha',
          'zipcode' =&gt; 'required|min:5|numeric',
          'temp_experience' =&gt; 'required|min:1|max:50|numeric',
          'temp_travel' =&gt;   'required|numeric',
          'temp_hourly_rate' =&gt; 'required|numeric|min:10'
      );
      $validator = Validator::make(Input::all(), $rules);
  
  
      // process the login
      if ($validator-&gt;fails()) {
          return Redirect::to('user/profile/' . $id . '/edit')
              -&gt;withErrors($validator)
              -&gt;withInput();
      } else {
          $software_checked = Input::get('software');
          if(is_array($software_checked))
          {
              $imploded_software = implode(',', $software_checked);
          }
  
  
          // store
          $user = User::find($id);
  
          $user-&gt;firstname       = Input::get('firstname');
          $user-&gt;lastname      = Input::get('lastname');
          $user-&gt;zipcode = Input::get('zipcode');
          $user-&gt;temp_experience = Input::get('temp_experience');
          $user-&gt;temp_travel = Input::get('temp_travel');
          $user-&gt;temp_hourly_rate = Input::get('temp_hourly_rate');
          $user-&gt;temp_software_experience = $imploded_software;
          if( $user-&gt;save() ) {
              if( $user-&gt;usertype == 'dental hygienist' &amp;&amp; !$user-&gt;license-&gt;temp_id ) {
                  $license = new License();
                  $license-&gt;temp_id = $id;
                  $license-&gt;license_expiration_date = Input::get('expiration_date');
  
                  $license-&gt;save();
              }
  
                  // redirect
                  Session::flash('message', 'Successfully updated profile!!');
                  return Redirect::to('user/profile/'.$id.'');
          }
      }
  
  }
  </code></pre>
  
  <p><strong>USER MODEL</strong></p>
  
  <pre><code>use Illuminate\Auth\UserInterface;
  use Illuminate\Auth\Reminders\RemindableInterface;
  
  class User extends Eloquent implements UserInterface, RemindableInterface {
  
  
  
      /**
       * The database table used by the model.
       *
       * @var string
       */
      protected $table = 'users';
  
      /**
       * The attributes excluded from the model's JSON form.
       *
       * @var array
       */
      protected $hidden = array('password');
  
      /**
       * Get the unique identifier for the user.
       *
       * @return mixed
       */
      public function getAuthIdentifier()
      {
          return $this-&gt;getKey();
      }
  
      /**
       * Get the password for the user.
       *
       * @return string
       */
      public function getAuthPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Get the e-mail address where password reminders are sent.
       *
       * @return string
       */
      public function getReminderEmail()
      {
          return $this-&gt;email;
      }
  
  
      public static function getTemps()
      {
  
         // Create a array of allowed types.
          $types = array('dental hygienist', 'dentist', 'dental assistance');
  
          // Get what type the user selected.
          $type = Input::get('temptype');
  
          //Get user location
          //$location = Input::get('zipcode');
  
  
          // Make sure it is a valid type.
          if(!in_array($type, $types))
          {
              return App::abort(500, "Invaild temptype.");
          }
  
        $temps =  DB::table('users')
              -&gt;join('availability', 'users.id', '=', 'availability.userid')
              -&gt;select('users.id', 'users.firstname', 'users.lastname', 'users.zipcode', 'users.salary', 'availability.dateavailable')
              -&gt;where('usertype', $type)
             //-&gt;where('zipcode', $location)
              -&gt;get();
  
          return $temps;
      }
  
      public function license()
      {
          return $this-&gt;hasOne('License');
      }
  </code></pre>
  
  <p><strong>Modified Controller</strong></p>
  
  <p>This works better but whenever i update the users date of expiration, state, or license number it works and it updates but there are duplicate entries in the database.....</p>
  
  <pre><code>if( $user-&gt;usertype == 'dental hygienist' || $user-&gt;usertype == 'dentist'  &amp;&amp; !$user-&gt;license-&gt;temp_id ) {
                      $license = new License();
                      $license-&gt;user_id = $id;
                      $license-&gt;temp_id = $id;
                      $license-&gt;license_expiration_date = Input::get('expiration_date');
                      $license-&gt;license_number = Input::get('license_number');
                      $license-&gt;license_state = Input::get('license_state');
  
                      $license-&gt;save();
                  }
  </code></pre>