Can't load image from structure field on page, doesn't loop to return each image

I have a structure field “Package Groups” all data loads except the image only getting the first image path repeated for each item in the loop. Any suggestions greatly received.

  <?php foreach ($page->packagegroups()->toStructure() as $value): ?>
    <li>
    <?php if($image = $value->cardimage()): ?>
      <img src="<?= $value->cardimage()->toFiles() ?>" alt="">
    <?php endif ?>

    <!-- Out puts path to one of the three images not looping and loading each of the three images -->
    <?= $value->cardimage()->toFiles() ?>

      <h2><?= $value->cardheading()->escape() ?></h2>
      <?= $value->cardtext()->kt() ?>
      <a href="<?= $link = $value->cardpagelink()->toLinkObject();?>"  class="p_button--primary"><?= $value->cardlink() ?></a>
    </li>
  <?php endforeach ?>

Blueprint:

    fields:
      packagegroups:
        label: Package Groups
        type: structure
        fields:
          cardimage:
            type: files
            layout: cards
          cardheading:
            label: Card Heading
            type: text
          cardtext:
            label: Card Text
            type: text
          cardlink:
            label: Card Link Text
            type: text
          cardpagelink:
            label: Card Page Link
            type: link
            width: 1/2
            linkTypes:
              - page

Must be toFile() for a single file, and you should check for the file object and then the fetch the url of the image, using the variable:

  <?php if($image = $value->cardimage()->toFile()): ?>
      <img src="<?= $image->url() ?>" alt="">
    <?php endif ?>

If you store multiple images, you need to loop through the collection, please also see the documentation for the files field.

Thanks, I now understand to load single image but this still even though inside the loop isn’t loading the other image files in the structured field. Just loading the same one each time (first image) The loop is loading all the text fields and looping through just not the image?

Ok, so multiple images, see docs here: Files | Kirby CMS

Thank you very much! now I understand even more :grinning: I’m still learning and obviously having one of those days! For the benefit of others here’s final code:

  <?php foreach ($page->packagegroups()->toStructure() as $value): ?>
    <li>
    <?php $images =  $value->cardimage()->toFiles(); foreach($images as $image): ?>
      <img src="<?= $image->url() ?>" alt="">
    <?php endforeach ?>
      <h2><?= $value->cardheading()->escape() ?></h2>
      <?= $value->cardtext()->kt() ?>
      <a href="<?= $link = $value->cardpagelink()->toLinkObject();?>"  class="p_button--primary"><?= $value->cardlink() ?></a>
    </li>
  <?php endforeach ?>