title: LARAVEL Eloquent: BelongsTo Relationship - Bulk update
body: <p>Imagine I want to lend multiple books to a customer.</p>
  
  <p>A customer can loan many books (hasMany) but a book can only be lend to one customer (BelongsTo).
  I want to change the BelongsTo side of the relationship for multiple books in one go.
  <a href="https://laravel.com/docs/7.x/eloquent-relationships#updating-belongs-to-relationships" rel="nofollow noreferrer">Laravel Documentation</a> says to use the associate() function. But as far as I can see, that's for changing one object at a time. So I have to use a loop as in massLoanOne. This generates a lot of queries (n + 1).</p>
  
  <p>Another way of doing this is by updating the customer_id on the Book model directly, as in massLoanTwo.
  This generates only 2 queries. But this is not the proper Object Oriented way. </p>
  
  <p>Is there a better way to update the BelongsTo side of this one-to-many (reverse) relationship bij using the Eloquent magic?</p>
  
  <pre><code>&lt;?php
      public function massLoanOne(MassLoanCustomerRequest $request)
      {
          foreach ($request-&gt;ids as $id) {
              Book::find($id)-&gt;customer()-&gt;associate(Customer::find($request-&gt;customer_id))-&gt;save();
          }
  
          return response()-&gt;noContent();
      }
  
      public function massLoanTwo(MassLoanCustomerRequest $request)
      {
          Book::whereIn('id', request('ids'))-&gt;update(['customer_id' =&gt; $request-&gt;customer_id]);
  
          return response()-&gt;noContent();
      }
  ?&gt;
  </code></pre>