title: Laravel 5.6 Eloquent tries to update non-existing column when nobody asked it to. (Many-to-Many relationship)
body: <p>I'm trying to implement a many-to-many relationship in a Laravel 5.6 app, using Eloquent ORM. I have <code>Customer</code> and <code>CustomerGroup</code> models, that correspond with the database tables of the same name, and a pivot table <code>customers_customergroups</code>. Relatively identical case is provided in Laravel's documentation with users and roles. The <code>customers_customergroups</code> table looks like this:</p>
  
  <pre><code>+-------------+------------------+------------+------------+
  | customer_id | customergroup_id | created_at | updated_at |
  +-------------+------------------+------------+------------+
  |    53210    |        2         |     --     |     --     |
  |    53210    |        4         |     --     |     --     |
  |    53211    |        3         |     --     |     --     |
  |    53211    |        4         |     --     |     --     |
  +-------------+------------------+------------+------------+
  </code></pre>
  
  <p>My <code>Customer</code> model has a method called <code>customerGroups()</code> which retrieves all groups the customer belongs to. </p>
  
  <pre><code>public function customerGroups()
  {
      return $this-&gt;belongsToMany('App\CustomerGroup', 'customers_customergroups', 'customer_id', 'customergroup_id');
  }
  </code></pre>
  
  <p>The retrieval works fine, but when I try to update <code>Customer</code> by passing a <code>Request</code> object as an array to the <code>update()</code> Eloquent method, and in the same repository method I have to update the <code>customers_customergroups</code> table, the latter updates normally, but <code>Customer::find($id)-&gt;update($request-&gt;toArray())</code> throws an Exception: </p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'groups' in 'field list'....</p>
  </blockquote>
  
  <p>There is no parameter named <code>groups</code> in the Request object passed to <code>Customer::find($id)-&gt;update();</code></p>
  
  <p>What happens here, why does this exception get thrown, what am I doing wrong, and how to do it properly?</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>Edit (posting the log requested in the comments below)</strong>:</p>
  
  <pre><code>[2019-05-14 13:42:11] local.INFO: array (
    'id' =&gt; 53211,
    'name' =&gt; 'CUSTOMER ONE',
    'surname' =&gt; NULL,
    'Telephone' =&gt; '0123-4567890',
    'email' =&gt; 'email@example.com',
    'Street' =&gt; '22 BAKER STR.',
    'City' =&gt; 'LONDON',
    'Postal_code' =&gt; '1234',
    'is_active' =&gt; '1',
    'website' =&gt; '',
  )
  </code></pre>
  
  <p><strong>Edit (Adding code requested by HCK):</strong></p>
  
  <p><strong>Customer.js</strong></p>
  
  <pre><code>$scope.CreateCustomer = function(isValid) {
      if (isValid)
      {
          PostData    = {
            'id': $scope.customer_id,
            ... all other parameters from the previous edit ...
      }
  
      if(($scope.izbraniGrupi).length &gt; 0)
      {
          PostData.izbraniGrupi = $scope.groupsSelected;
      }
  
      $http({
              method : 'PUT', // Laravel controller is a resource
              url : 'customers',
              data : PostData
          }).then(function mySuccess(response) {
             // Other stuff that happens on responseCode == 200.
          });
  };
  </code></pre>
  
  <p><strong>CustomerController.php</strong></p>
  
  <pre><code>// $this-&gt;repository is defined normally in __construct() and works as supposed to.
  public function update(Request $request, $id)
  {
      try {
          $this-&gt;repository-&gt;update($id, $request);
      } catch(\Exception $e) {
          return response()-&gt;json([
              'message'  =&gt;  $e-&gt;getMessage()
          ]);
      }
  
      return response()-&gt;json([
          'message' =&gt;  'Customer updated'
      ]);
  }
  </code></pre>
  
  <p><strong>CustomerRepository.php</strong></p>
  
  <pre><code>public function update($id, $data)
  {
      if(isset($data['izbraniGrupi'])) {
  
          DB::table('customers_customergroups')-&gt;where(['customer_id' =&gt; $id])-&gt;delete();
  
          foreach ($data['izbraniGrupi'] as $group) {
  
              DB::table('customers_customergroups')-&gt;insert([
                  'customer_id' =&gt; $id,
                  'customergroup_id' =&gt; $group['id']
              ]);
          }
  
          unset($data['izbraniGrupi']);
      }
  
      Customer::find($id)-&gt;update($data-&gt;toArray());
  }
  </code></pre>