<p>Same as you would access it from a Twig <a href="https://craftcms.com/docs/categories-fields#templating" rel="nofollow">template</a>.  If you're positive there will always be one category selected, then this would work:</p>

<pre><code>'transformer' =&gt; function(EntryModel $entry) {
    return [
        'category' =&gt; (string)$entry-&gt;categoryHandle[0],
    ];
},
</code></pre>

CC BY-SA 3.0
2015-12-20T16:12:33.047Z
yeah, I'd thought I could have done that but nothing comes from it.
Steven Grant
CC BY-SA 3.0
2015-12-20T18:52:36.723Z
Are you sure you have the correct `$entry`? What do you get if you dump `$entry->categoryHandle[0]` from a normal Twig template?
Brad Bell
CC BY-SA 3.0
2015-12-20T19:35:10.577Z
Should have clarified, this is for all jobs, not a single entry endpoint. I found that ```'category' => (string) $entry->jobCategory[0],``` works though
Steven Grant
CC BY-SA 3.0
2015-12-20T21:53:23.277Z
Weird... CategoryModel's `__toString()` magic method should be getting called there.  Regardless, I'll update my answer.
Brad Bell
CC BY-SA 3.0
2015-12-20T02:48:35.460Z
2015-12-20T21:53:34.910Z
2015-12-20T21:53:34.910Z
Brad Bell
<p>The category count is only ever 1 and I need to output the name of the category the entry belongs to.</p>

<p>Would there need to be a cross-over between the EntryModel and CategoryModel?</p>

<pre><code>'transformer' =&gt; function(EntryModel $entry) {
                return [
                    'title' =&gt; $entry-&gt;title,
                    'url' =&gt; $entry-&gt;url,
                    'jsonUrl' =&gt; UrlHelper::getUrl("jobs/{$entry-&gt;id}.json"),
                    'postDate' =&gt; $entry-&gt;postDate-&gt;getTimestamp() * 1000,
                    'id' =&gt; $entry-&gt;id,
                    'jobOverview' =&gt; (string) $entry-&gt;jobOverview,
                    'category' =&gt; $entry-&gt;jobCategory[0],
                ];
            },
</code></pre>

<json><plugin-elementapi>
How do I access the category of an entry with Element API?
# How do I access the category of an entry with Element API?

(Asked by: username_0 on 2015-12-19)

The category count is only ever 1 and I need to output the name of the category the entry belongs to.
Would there need to be a cross-over between the EntryModel and CategoryModel?
'transformer' => function(EntryModel $entry) {
                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'jsonUrl' => UrlHelper::getUrl("jobs/{$entry->id}.json"),
                    'postDate' => $entry->postDate->getTimestamp() * 1000,
                    'id' => $entry->id,
                    'jobOverview' => (string) $entry->jobOverview,
                    'category' => $entry->jobCategory[0],
                ];
            },

**Tags:** json, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2015-12-20. Score: 3 | Accepted answer

Same as you would access it from a Twig template.  If you're positive there will always be one category selected, then this would work:
'transformer' => function(EntryModel $entry) {
    return [
        'category' => (string)$entry->categoryHandle[0],
    ];
},

> **Comments:**
> **username_0**: yeah, I'd thought I could have done that but nothing comes from it.  
> **username_1**: Are you sure you have the correct `$entry`? What do you get if you dump `$entry->categoryHandle[0]` from a normal Twig template?  
> **username_0**: Should have clarified, this is for all jobs, not a single entry endpoint. I found that ```'category' => (string) $entry->jobCategory[0],``` works though  
> **username_1**: Weird... CategoryModel's `__toString()` magic method should be getting called there.  Regardless, I'll update my answer.  

---

