<p>If you want to show all ingredients related to the current entry you'd use the Single Entry Endpoint to pass the entry into the <code>relatedTo</code> param.</p>

<pre><code>'ingredients/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
  return [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; [
        'section' =&gt; 'ingredients',
        'relatedTo' =&gt; $entryId
    ],
    'transformer' =&gt; function(EntryModel $entry) {
      return [
        'title' =&gt; $entry-&gt;title,
        'url' =&gt; $entry-&gt;url
      ];
    },
  ];
},
</code></pre>

CC BY-SA 3.0
2016-08-02T13:38:57.017Z
If I understand correctly here we've returned an array of all related entries titles per single entry from section _ingredients_.  Now I'm thinking how this should point me in the right direction when I'd like to show related entries from the _ingredients_ section of a current entry.
Dominik Krulak
CC BY-SA 3.0
2016-08-02T13:51:22.747Z
I expect I failed to understand your original requirements. Can you update your original question with some more details about the site's structure and the entry you're talking about? Is the entry you're outputting these ingredients on also a related entry of an ingredient?
Luke Pearce
CC BY-SA 3.0
2016-08-02T13:53:36.143Z
I'm on it right now and thanks for the grammar :) @Luke Pearce
Dominik Krulak
CC BY-SA 3.0
2016-08-02T14:40:52.283Z
This question links me to [this question](http://craftcms.stackexchange.com/questions/11354/passing-multiple-parameters-to-the-element-api)
Dominik Krulak
CC BY-SA 3.0
2016-08-02T15:06:47.300Z
Updated my original answer. This will return all ingredients related to the entry of whose ID you use in the endpoint.
Luke Pearce
CC BY-SA 3.0
2016-08-02T15:45:59.410Z
Sorry for the late update on your answer. I was trying to comprehend it. Thank you @Luke Pearce, it works :)
Dominik Krulak
CC BY-SA 3.0
2016-08-02T11:54:40.767Z
2016-08-02T15:05:11.903Z
2016-08-02T15:05:11.903Z
Luke Pearce
<p>By following the <a href="https://github.com/pixelandtonic/ElementAPI#examples" rel="nofollow">ElementAPI plugin documentation</a> I was able to set up pagination of entries from the <em>ingredients</em> section on an entry page. Entries from the <em>ingredients</em> section have a relation field called <em>relatedEntries</em>.</p>

<p>Sections structures involved - set as an example:</p>

<ol>
<li><em>ingredients</em></li>
<li><em>drinks</em></li>
</ol>

<p><strong>Desired output would be to show entries related to currently loaded entry from <em>drinks</em> section.</strong></p>

<p>So when I'm at <em>site.com/drink-entry-alfa</em> I'd like to display all related entries from section <em>ingredients</em> to that current entry.</p>

<p>Using Twig I'd request current <code>entry.id</code> and use the <code>.relatedTo()</code> parameter to get related entries.</p>

<p>Here is the example from ElementAPI's documentation:</p>

<pre><code>&lt;?php
namespace Craft;

return [
  'endpoints' =&gt; [
    'ingredients.json' =&gt; [
      'criteria' =&gt; ['section' =&gt; 'ingredients'],
      'elementsPerPage' =&gt; 10,
      'transformer' =&gt; function(EntryModel $entry) {
        return [
          'title' =&gt; $entry-&gt;title
        ];
      },
    ],
  ],
];
</code></pre>

CC BY-SA 3.0
2016-08-02T14:23:53.343Z
So you'd like to only show ingredients related to the current entry? Or you want to show all ingredients and if that ingredient is related to the current entry you want to get all entries related to that ingredient? Can you add an example of the structure using Drink A, B, C etc and Ingredient A, B, C?
Luke Pearce
CC BY-SA 3.0
2016-08-02T14:31:35.207Z
Just to show ingredients related to the current entry.
Dominik Krulak
CC BY-SA 3.0
2016-08-02T14:38:42.080Z
Also, @Luke Pearce you can see a little demonstration bellow the bold section in this question.
Dominik Krulak
<plugin-elementapi>
Display related entries on entry page using ElementAPI
# Display related entries on entry page using ElementAPI

(Asked by: username_0 on 2016-08-02)

By following the ElementAPI plugin documentation I was able to set up pagination of entries from the ingredients section on an entry page. Entries from the ingredients section have a relation field called relatedEntries.
Sections structures involved - set as an example:

ingredients
drinks

Desired output would be to show entries related to currently loaded entry from drinks section.
So when I'm at site.com/drink-entry-alfa I'd like to display all related entries from section ingredients to that current entry.
Using Twig I'd request current entry.id and use the .relatedTo() parameter to get related entries.
Here is the example from ElementAPI's documentation:
 [
    'ingredients.json' => [
      'criteria' => ['section' => 'ingredients'],
      'elementsPerPage' => 10,
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title
        ];
      },
    ],
  ],
];

**Tags:** plugin-elementapi
> **Comments:**
> **username_1**: So you'd like to only show ingredients related to the current entry? Or you want to show all ingredients and if that ingredient is related to the current entry you want to get all entries related to that ingredient? Can you add an example of the structure using Drink A, B, C etc and Ingredient A, B, C?  
> **username_0**: Just to show ingredients related to the current entry.  
> **username_0**: Also, @Luke Pearce you can see a little demonstration bellow the bold section in this question.  

---

## 1 answers

### Answer by username_1 on 2016-08-02. Score: 2 | Accepted answer

If you want to show all ingredients related to the current entry you'd use the Single Entry Endpoint to pass the entry into the relatedTo param.
'ingredients/.json' => function($entryId) {
  return [
    'elementType' => 'Entry',
    'criteria' => [
        'section' => 'ingredients',
        'relatedTo' => $entryId
    ],
    'transformer' => function(EntryModel $entry) {
      return [
        'title' => $entry->title,
        'url' => $entry->url
      ];
    },
  ];
},

> **Comments:**
> **username_0**: If I understand correctly here we've returned an array of all related entries titles per single entry from section _ingredients_.  Now I'm thinking how this should point me in the right direction when I'd like to show related entries from the _ingredients_ section of a current entry.  
> **username_1**: I expect I failed to understand your original requirements. Can you update your original question with some more details about the site's structure and the entry you're talking about? Is the entry you're outputting these ingredients on also a related entry of an ingredient?  
> **username_0**: I'm on it right now and thanks for the grammar :) @Luke Pearce  
> **username_0**: This question links me to [this question](http://craftcms.stackexchange.com/questions/11354/passing-multiple-parameters-to-the-element-api)  
> **username_1**: Updated my original answer. This will return all ingredients related to the entry of whose ID you use in the endpoint.  
> **username_0**: Sorry for the late update on your answer. I was trying to comprehend it. Thank you @Luke Pearce, it works :)  

---

