Difficulty merging structures into a collection

<?php
    // initialize a new structure object
    $structure = new Structure();
    // get all pages with structure field
    $productions = $site->find('page://YDLYvueOhZwB3glM')->children()->listed();
    // loop through all pages and add structure items to the structure object
    foreach ($productions as $production):
      $items = $production->playdates()->toStructure();
      // we add them to our new structure
      $structure->add($items);
    endforeach;
  // only when this is done, we move on to the next step, looping through the newly created structure object, which we can now also sort:
?>
<ul>
  <?php foreach ($structure->sortBy('playdate', 'desc') as $item): ?>
    <li>
      <span><?= $item->playdate()->toDate('EEEE d MMMM Y – H:mm') ?></span>
      <span><?= $production->title() ?></span>
      <span><?= $item->location() ?></span>
      <span><?= $item->city() ?></span>
      <?php if ($item->ticketlink()->isNotEmpty()): ?>
        <a href="<?= $item->ticketlink() ?>">tickets</a>
      <?php endif ?>
    </li>
  <?php endforeach ?>
</ul>

That’s why it is so important to get it right in your head first, before you even start to write code. It’s all about logic and doing steps in the right order. Try to understand why this works and yours doesn’t, and where you went wrong. That’s the first step to your bright future in coding :wink:

1 Like