toStructure comes up null

Hi, this is my first question here.

I read through various forum posts, but none of the solutions have worked for me. My toStructure() code keeps coming up null.

This is the toStructure field in my blueprint:

  stories:
    label: Stories
    type: structure
    fields:
      story:
        label: Story
        type: text
      link:
        label: Link
        type: url

which produces this format:

Stories:
- 
  story: There once was a dog who drove a car.
  link: 'https://example1.com'
- 
  story: There once was a cat who flew a plane.
  link: 'https://example2.com'

I tried the code from $field->toStructure() and changed it like so:

<?php foreach (page('history')->children()->stories()->toStructure() as $story): ?>

   <a href="<?= $story->link() ?>">
      <?= $story->story() ?>
   </a>

<?php endforeach ?>

but it comes up null. I can get regular field data to show up in the template, but not the structured field data.

How to do render the data from the structure field in the template?

You cannot call a field (stories()) and a collection of pages, if you want to call the field, you first have to loop through the pages, then call stories()->toStructure() on the single page of the collection.

So

<?php foreach (page('history')->children() as $p): ?>

    <?php foreach ($p->stories()->toStructure() as $story): ?>
        <a href="<?= $story->link() ?>">
          <?= $story->story() ?>
       </a>

    <?php endforeach ?>
<?php endforeach ?>

That works! Thank you so much!!