Using ->has() on structure object?

I need to check if a certain object is in a certain structure object, and I am using ->has() but I am running into glitches, is it perhaps not correct to use has() in this particular case ?

The use-case. I produce a series of collections in the controller, from a single structure field:

$allFairs = page('fairs')->fairs()->toStructure()->sortBy('openingdate','desc');

$upcomingFairs = $allFairs->filterBy('openingdate', 'date >', 'today');
$currentFairs = $allFairs->filterBy('openingdate', 'date <=', 'today')->filterBy('closingdate', 'date >=', 'today');
$pastFairs = $allFairs->filterBy('closingdate', 'date <', 'today');

Then, in the templates/snippets, at some point I need to check if a certain fair is in one of these collections, and I do it with has() method and it does not fail, but the results I am getting do not seem to be correct. For example this code:

<?php foreach($upcomingFairs as $up): ?>
  <?= $up->name() ?><br />
<?php endforeach ?>

Echoes this result:

upcoming Gongorr

But the following code

  <?php $allFairsByYear = $allFairs->group(function($p) {return $p->openingdate()->toDate('Y');}) ?>	
  <?php foreach ($allFairsByYear as $year => $fairs): ?>
    <div><?= $year ?></div>
    <?php foreach ($fairs as $fair): ?>
      <?= $fair->name() ?><br />
      <?= $upcomingFairs->has($fair) ? 'upcoming' : 'not upcoming' ?><br />
      <?= $currentFairs->has($fair) ? 'current' : 'not current' ?><br />
    <?php endforeach ?>
  <?php endforeach ?>


not upcoming
not current

Is the code wrong? Can’t I use has()? or am I not reading well ?

Thank you

From looking at the source code I suspect the problem here is that a structureObject doesn’t have an id, so the method probably doesn’t work as expected.


 <?php foreach ($fairs as $fair): ?>
      <?= $fair->name() ?><br />
      <?= $upcomingFairs->findBy('name', $fair->name()) ? 'upcoming' : 'not upcoming' ?><br />
      <?= $currentFairs->findBy('name', $fair->name()) ? 'current' : 'not current' ?><br />
    <?php endforeach ?>

Thank you.

Would there be any reccomended way to check for the presence of the objects within the structure object collection?

See my edited post above.