title: Deep relations, *child* relation depends on *great-grandparent* id - with | load - Laravel/Eloquent
body: <p>I have this DB/Model structure (example):</p>
  <p>*<em>Food Category ------------ (id, name)</em> - *irrelevant</p>
  <p><strong>Foods</strong> ---------------------- (id, food_category_id, name, ...)</p>
  <p><strong>Accessory Groups</strong> --------- (id, name, ...)</p>
  <p><em>Foods_Accessory Groups - (food_id, acc_group_id)</em> - <em>pivot table</em></p>
  <p><strong>Accessories</strong>            ---------------- (id, acc_group_id, name, ...)</p>
  <p><em>Foods_Accessories_Prices - (food_id, accessory_id, <strong>price</strong>)</em> - <em>pivot table</em></p>
  <p><strong>Models</strong></p>
  <ul>
  <li>FoodCategory (hasMany: foods)</li>
  <li>Food (hasMany: foodAccessoriesGroup)</li>
  <li>FoodAccessoriesGroup (hasMany: foodAccessory)</li>
  <li><strong>problematic</strong>: *FoodAccessory (<strong>belongsToMany | belongsTo</strong>: food_prices | food_price ------
  (pivot: [price], table: food_accessory_prices, model: Food)</li>
  </ul>
  <p><strong>Test:</strong>
  <a href="https://i.stack.imgur.com/IKa1H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IKa1H.png" alt="tables" /></a></p>
  <pre><code>**foods**
  | id       | name |
  | -------- | ---- |
  | 1        | Pork |
  | 2        | Eggs |
  
  **accessory_groups**
  | id     | name |
  | -------| -----|
  | 1      | Sauce|
  
  foods_accessory_groups
  | food_id  | accessory_group_id |
  | -------- | --------------     |
  | 1        | 1                  |
  | 2        | 1                  |
  
  **accessories**
  | id       | name           | acc_group_id |
  | -------- | -------------- | - |
  | 1        | Mayo           | 1 |
  | 2        | Ketchup        | 1 |
  
  **foods_accessories_prices**
  | food_id  | accessory_id   | price |
  | -------- | -------------- | -     |
  | 1        | 1              | 200   |
  | 2        | 1              | 300   |
  | 1        | 2              | 250   |
  | 2        | 2              | 350   |
  </code></pre>
  <p><strong>Code Example:</strong></p>
  <pre><code>// get all food and relations
  $foodCategories = FoodCategory::with([
    'foods',
    'foods.accessory_groups',
    'foods.accessory_groups.accessories',
    'foods.accessory_groups.accessories.food_prices' or
    'foods.accessory_groups.accessories.food_price' =&gt; function ($query) {
      // how to filter food prices by great-grandparent relation food (by food_id)
      // I know that it can't be done here, but where and how
    }
  ])-&gt;get();
  </code></pre>
  <p>Accessory <em>dynamic price</em> is depended on Food (great-grandparent relation). <strong>What is best practice and most optimize solution for this problem?</strong> Post-process or Laravel Eloquent has solution without too much trouble</p>
  <p><a href="https://i.stack.imgur.com/INTSv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/INTSv.png" alt="enter image description here" /></a></p>
  <p><strong>food (id = 1, Pork)</strong> -&gt; has foods_accessory_groups(id = 1, Sauce) -&gt; has accessories (mayo and ketchup) where <strong>mayo price is 200</strong>, and ketchup price is 300.</p>
  <p><strong>food (id = 2, Eggs)</strong> - ... - <strong>mayo price is 250</strong>, and ketchup price is 350.</p>
  <p><strong>API response</strong> from example:</p>
  <p><a href="https://i.stack.imgur.com/WxRZD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WxRZD.png" alt="enter image description here" /></a></p>