Element-API: Access the first url of an asset array I have an asset field in my current project with the handle postImage, containing multiple image files. In my element-api endpoint, I wish to access the url of the first file only.

I tried the following code:

$imgs = $entry-&gt;postImage;
$imgUrl = $imgs[0]-&gt;getUrl();


The resulting console error message says:

500 (Call to a member function getUrl() on string)


Looping through the array works fine, but I don't want all image urls, just the first one.

It's probably very easy, but that ties a knot my brain. I don't understand where the array is being converted to a string, can anyone hint me to the solution?

Thanks a lot!
you could use array_values() to fetch the first image:

...
      'transformer' =&gt; function (Entry $entry) {
        foreach ($entry-&gt;images as $image) {
          $images[] = $image-&gt;url;
        }
        return [
          'images' =&gt;  array_values($images)[0],
        ];
      },
...


or use reset()

...
      'transformer' =&gt; function (Entry $entry) {
        foreach ($entry-&gt;images as $image) {
          $images[] = $image-&gt;url;
        }
        return [
          'images' =&gt;  reset($images),
        ];
      },
...