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): ?>
upcoming
<?= $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 ?>
Echoes:
Gongorr
not upcoming
not current
Is the code wrong? Can’t I use has()? or am I not reading well ?
Thank you