title: Laravel 3: Model function with $this->save() cause error with Eloquent update
body: <p>I've spent far too long on this and it just isn't working, so I've given up and will rewrite it with a Fluent update method instead.</p>
  
  <p>But I wanted to post it here and see if anyone else has encountered this before.</p>
  
  <pre><code>// TEST CONTROLLER
  $user = User::find(29);
  // This user has a related subscription but it's lacking all of the braintree data that I need 
  // So as a fallback I need to make sure that if the braintree_customer_id column is empty
  // then we query braintree with an email lookup for the customer
  // and then fill in the missing data.
  
  $data = $user-&gt;subscription-&gt;braintree_customer;
  var_dump($data);
  
  // SUBSCRIPTION MODEL
  public function get_braintree_customer()
  {
      $braintree_customer_id = $this-&gt;braintree_customer_id;
      if (empty($braintree_customer_id))
      {
          // IF THE CUSTOMER DOESN"T HAVE ID SET THEN CALL ANOTHER FUNCTION IN THE MODEL
          $data = $this-&gt;customer_from_braintree;
          return $data;
      }
      $data = Braintree_Customer::find($braintree_customer_id);
      return $data;
  }
  public function get_customer_from_braintree()
  {
      $data = FALSE;
      $braintree_customer = Braintree_Customer::search(array(
          Braintree_CustomerSearch::email()-&gt;is($this-&gt;user-&gt;email),
          // Braintree_CustomerSearch::email()-&gt;is('bleugh@pixelapes.com'),
      ));
      if (!empty($braintree_customer-&gt;_ids))
      {
          $braintree_customer_id = $braintree_customer-&gt;_ids[0];
          $data = Braintree_Customer::find($braintree_customer_id);
          $braintree_subscription = $data-&gt;creditCards[0]-&gt;subscriptions[0];
  
              $this-&gt;braintree_customer_id = $data-&gt;id;
              $this-&gt;braintree_subscription_id = $braintree_subscription-&gt;id;
              $this-&gt;braintree_payment_method_token = $braintree_subscription-&gt;paymentMethodToken;
              $this-&gt;braintree_plan_id = $braintree_subscription-&gt;planId;
              $this-&gt;save();
      }
      return $data;
  }}
  // IF I TRY USING $this-&gt;save() I get this error
  // Even though I haven't tried to create a new Subscription - just update the old one
      Unhandled Exception
  
      Message:
  
      SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 'PRIMARY'
  
      SQL: INSERT INTO `subscriptions` (`id`, `user_id`, `braintree_customer_id`, `braintree_subscription_id`, `braintree_payment_method_token`, `braintree_plan_id`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  
      Bindings: array (
        0 =&gt; 2,
        1 =&gt; 29,
        2 =&gt; '6056122',
        3 =&gt; '',
        4 =&gt; '',
        5 =&gt; '',
        6 =&gt; '2013-10-15 06:32:21',
        7 =&gt; '2013-10-15 06:32:21',
      )
  
  
  Location:
  
  path\to\laravel\database\connection.php on line 263
  Stack Trace:
  
  #0 path\to\laravel\database\connection.php(183): Laravel\Database\Connection-&gt;execute('INSERT INTO `su...', Array)
  #1 path\to\laravel\database\query.php(824): Laravel\Database\Connection-&gt;query('INSERT INTO `su...', Array)
  #2 [internal function]: Laravel\Database\Query-&gt;insert_get_id(Array, 'id')
  #3 path\to\laravel\database\eloquent\query.php(267): call_user_func_array(Array, Array)
  #4 path\to\laravel\database\eloquent\model.php(402): Laravel\Database\Eloquent\Query-&gt;__call('insert_get_id', Array)
  #5 path\to\laravel\database\eloquent\model.php(402): Laravel\Database\Eloquent\Query-&gt;insert_get_id(Array, 'id')
  #6 path\to\application\models\subscription.php(53): Laravel\Database\Eloquent\Model-&gt;save()
  #7 path\to\laravel\database\eloquent\model.php(697): Subscription-&gt;get_customer_from_braintree()
  #8 path\to\application\models\subscription.php(70): Laravel\Database\Eloquent\Model-&gt;__get('customer_from_b...')
  #9 path\to\laravel\database\eloquent\model.php(697): Subscription-&gt;get_braintree_customer()
  #10 path\to\application\controllers\test.php(10): Laravel\Database\Eloquent\Model-&gt;__get('braintree_custo...')
  #11 [internal function]: Test_Controller-&gt;action_index()
  #12 path\to\laravel\routing\controller.php(325): call_user_func_array(Array, Array)
  #13 path\to\laravel\routing\controller.php(285): Laravel\Routing\Controller-&gt;response('index', Array)
  #14 path\to\laravel\routing\controller.php(165): Laravel\Routing\Controller-&gt;execute('index', Array)
  #15 path\to\laravel\routing\route.php(153): Laravel\Routing\Controller::call('test@index', Array)
  #16 path\to\laravel\routing\route.php(124): Laravel\Routing\Route-&gt;response()
  #17 path\to\laravel\laravel.php(167): Laravel\Routing\Route-&gt;call()
  #18 path\to\public\index.php(34): require('D:\Work\www\xam...')
  #19 {main}
  </code></pre>
  
  <p>I've tried with every single Eloquent update method I can think of, I've tried directly using the set_attribute() function in the second model function, I've tried creating setters for each of the columns I want to update and calling those setters, but then the function succeeds but the row is NOT updated.</p>
  
  <p>However if I try to manually run the setters in a test controller they work fine?!</p>
  
  <p>I've tried calling the model function directly from a controller and it still causes an error.</p>
  
  <p>The weirdest thing is that in my user model I have done this exact approach before where I set attributes on $this and then run $this->save(); and it works fine.</p>
  
  <p>Baffled!</p>