Has many through many-to-many
<p>I have the following table layout:</p>

<pre><code>deals:
- id
- price

products:
- id
- name

deal_product:
- id
- deal_id
- product_id

metrics:
- id
- name

metric_product:
- id
- metric_id
- product_id
- value
</code></pre>

<p><code>products</code> and <code>metrics</code> have a many-to-many relationship with a pivot column of value.</p>

<p><code>deals</code> and <code>products</code> also have a many-to-many relationship.</p>

<p>I can get metrics for a product with <code>$product-&gt;metrics</code>, but I want to be able to get all metrics for all products related to a deal, so I could do something like this: <code>$deal-&gt;metrics</code>.</p>

<p>I currently have the following in my <code>Deal</code> model:</p>

<pre><code>public function metrics()
{
    $products = $this-&gt;products()-&gt;pluck('products.id')-&gt;all();

    return Metric::whereHas('products', function (Builder $query) use ($products) {
        $query-&gt;whereIn('products.id', $products);
    });
}
</code></pre>

<p>But this doesn't return a relationship, so I cannot eager load it or get related models from it.</p>

<p>It needs to be in relationship format, because they need to be eager loaded for my use case.</p>

<p>Thanks for your help!</p>