title: Load laravel eloquent model withCount of related model
body: <p>Given I have two eloquent models: Booking and Customer. </p>
  
  <p>When I list all Bookings along with the respective Customer, I also want to show the amount of Bookings the respective customer has in total (count of this Booking + all other bookings).</p>
  
  <p>Example output:</p>
  
  <ul>
  <li>Booking1: Customer A (has 20 Bookings total)</li>
  <li>Booking2: Customer B (has 10 Booking total)</li>
  <li>Booking3: Customer C (<strong>VIP</strong>: has 100 Bookings total)</li>
  </ul>
  
  <p>In order to avoid the n+1 problem (one additional query per booking while showing this), I'd like to eager load the <code>bookingsCount</code> for the Customer.</p>
  
  <p>The relations are:</p>
  
  <p>Booking: <code>public function customer() { return $this-&gt;belongsTo(Customer::class) }</code></p>
  
  <p>Customer: <code>public function bookings() { return $this-&gt;hasMany(Booking::class) }</code></p>
  
  <p><strong>Example for querying the Bookings with eager loading</strong></p>
  
  <p>Working, but without eager loading of the bookingsCount:</p>
  
  <pre><code>Booking::whereNotCancelled()-&gt;with('customer')-&gt;get();
  </code></pre>
  
  <p>Not working:</p>
  
  <pre><code>Booking::whereNotCancelled()-&gt;with('customer')-&gt;withCount('customer.bookings')-&gt;get();
  </code></pre>
  
  <p>I learned, that you cannot use <code>withCount</code> on fields of related models, but you can create a <code>hasManyThrough</code> relation and call <code>withCount</code> on that relation, e.g. <code>Booking::whereNotCancelled()-&gt;withCount('customerBookings');</code> (<a href="https://stackoverflow.com/questions/39633691/laravel-5-3-withcount-nested-relation">see accepted answer here</a>).</p>
  
  <p>However: This doesn't work. I guess, it's because a Booking belongsTo a Customer and a Customer hasMany Bookings.  </p>
  
  <p><strong>Here's the hasManyThrough relation of class Booking</strong></p>
  
  <pre><code>public function customerBookings()
  {
      // return the bookings of this booking's customer
      return $this-&gt;hasManyThrough(Booking::class, Customer::class);
  }
  </code></pre>
  
  <p><strong>Here's the failing test for hasManyThrough</strong></p>
  
  <pre><code>/**
   * @test
   */
  public function it_has_a_relationship_to_the_customers_bookings()
  {
      // Given we have a booking
      $booking = factory(Booking::class)-&gt;create();
      // And this booking's customer has other bookings
      $other = factory(Booking::class,2)-&gt;create(['customer_id' =&gt; $booking-&gt;customer-&gt;id]);
      // Then we expect the booking to query all bookings of the customer
      $this-&gt;assertEquals(3, Booking::find($booking-&gt;id)-&gt;customerBookings()-&gt;count());
  }
  </code></pre>
  
  <p><strong>Reported error</strong></p>
  
  <pre><code>no such column: customers.booking_id (SQL: select count(*) as aggregate from "bookings" inner join "customers" on "customers"."id" = "bookings"."customer_id" where "customers"."booking_id" = efe51792-2e9a-4ec0-ae9b-a52f33167b66)
  </code></pre>
  
  <p>No surprise. There is no such column <code>customer.booking_id</code>. </p>
  
  <p><strong>The Question</strong></p>
  
  <p>Is the intended behavior even possible in this case? If so, how would I eager load the booking's customer's total count of bookings?</p>