<p>Why don't you just check if there is at least one photo? Something along the lines of</p>

<pre><code>'transformer' =&gt; function(EntryModel $entry) {
    return [
        'ID' =&gt; $entry-&gt;id,
        'Year' =&gt; $entry-&gt;year,
        'Manufacturer' =&gt; $entry-&gt;manufacturer,
        'Model' =&gt; $entry-&gt;model,
        'Category' =&gt; (string)$entry-&gt;category[0],
        'Pics' =&gt; !empty($entry-&gt;listingPhotos) ? $entry-&gt;listingPhotos[0]-&gt;url : null,
    ];
}
</code></pre>

CC BY-SA 3.0
2017-02-10T14:51:20.197Z
I tried something similar to this earlier but it didn't work. I was able to get your code to work, but I had to add [0]-> to the argument:
`'Pics' => !empty($entry->listingPhotos[0]->url) ? $entry->listingPhotos[0]->url : null,`
Noah Yamen
CC BY-SA 3.0
2017-02-10T09:51:11.640Z
2017-02-10T09:51:11.640Z
Andris Sevcenko
<p>These keeps erroring out if any of the entries' do not have any assets in the 'listingPhotos' field. However, IF all entries have at least 1 photo in the 'listingsPhotos' field, it works. </p>

<p>The Error Message: Trying to get property of non-object </p>

<pre><code>namespace Craft;

return [
    'endpoints' =&gt; [
        'trucks.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; ['section' =&gt; 'trucks'],
            'paginate' =&gt; false,
            'transformer' =&gt; function(EntryModel $entry) {
                return [
                    'ID' =&gt; $entry-&gt;id,
                    'Year' =&gt; $entry-&gt;year,
                    'Manufacturer' =&gt; $entry-&gt;manufacturer,
                    'Model' =&gt; $entry-&gt;model,
                    'Category' =&gt; (string)$entry-&gt;category[0],
                    'Pics' =&gt; $entry-&gt;listingPhotos[0]-&gt;url,
                ];
            },
        ],
    ]
];
</code></pre>

<entries><assets><plugin-elementapi>
ElementAPI: Error's out if entry doesn't have assets
# ElementAPI: Error's out if entry doesn't have assets

(Asked by: username_0 on 2017-02-09)

These keeps erroring out if any of the entries' do not have any assets in the 'listingPhotos' field. However, IF all entries have at least 1 photo in the 'listingsPhotos' field, it works. 
The Error Message: Trying to get property of non-object 
namespace Craft;

return [
    'endpoints' => [
        'trucks.json' => [
            'elementType' => 'Entry',
            'criteria' => ['section' => 'trucks'],
            'paginate' => false,
            'transformer' => function(EntryModel $entry) {
                return [
                    'ID' => $entry->id,
                    'Year' => $entry->year,
                    'Manufacturer' => $entry->manufacturer,
                    'Model' => $entry->model,
                    'Category' => (string)$entry->category[0],
                    'Pics' => $entry->listingPhotos[0]->url,
                ];
            },
        ],
    ]
];

**Tags:** entries, assets, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2017-02-10. Score: 3 | Accepted answer

Why don't you just check if there is at least one photo? Something along the lines of
'transformer' => function(EntryModel $entry) {
    return [
        'ID' => $entry->id,
        'Year' => $entry->year,
        'Manufacturer' => $entry->manufacturer,
        'Model' => $entry->model,
        'Category' => (string)$entry->category[0],
        'Pics' => !empty($entry->listingPhotos) ? $entry->listingPhotos[0]->url : null,
    ];
}

> **Comments:**
> **username_0**: I tried something similar to this earlier but it didn't work. I was able to get your code to work, but I had to add [0]-> to the argument: `'Pics' => !empty($entry->listingPhotos[0]->url) ? $entry->listingPhotos[0]->url : null,`  

---

