title: Query Laravel relations, products, storehouses and sales
body: <p>I'm using Laravel to manage products and sales in different storehouses, there are four tables: products, storehouses, sales and saleItems witch look like this when simplified:</p>
  <p><strong>Storhouse</strong> id, adress, ...</p>
  <p><strong>Product</strong> id, name, ...</p>
  <p><strong>Sale</strong> id, store_id, ...</p>
  <p><strong>SaleItems</strong> id, product_id, sale_id, qty, ...</p>
  <p>The question is how to get a list of the products and the count of sales for each of them in each storehouse, anyone has an idea how to do that?</p>
  <p>Here are the models:</p>
  <pre><code>class Product extends Model 
  {
      public function sales(){
        return $this-&gt;hasMany('App\Models\SaleItem');
      }
  }
  
  class Sale extends Model
  {
    public function storehouse(){
      return $this-&gt;belongsTo('App\Models\Storehouse');
    }
    public function items(){
      return $this-&gt;hasMany('App\Models\Saleitem');
    }
  }
  
  class SaleItem extends Model
  {
    public function sale(){
      return $this-&gt;belongsTo('App\Models\Sale');
    }
    public function product(){
      return $this-&gt;belongsTo('App\Models\Product');
    }
  }
  
  class Storehouse extends Model
  {
    public function sales() {
      return $this-&gt;hasMany('App\Models\Sale');
    }
  }
  </code></pre>