I wish you a happy new year!
In a previouse question, I have got the confirmation to filter items in a relation to the actual time with:
<?php foreach (page('concert')->children()->listed()->filter(function ($child) { return $child->date()->toDate() < time(); }) as $concert_item): ?>
...
<?php endforeach ?>
Now I need a limitation of visible items too. But the attribute ->limit(5) to reduce the items to five entries for example is not accepted:
<?php foreach (page('concert')->children()->listed()->limit(5)->filter(function ($child) { return $child->date()->toDate() < time(); }) as $concert_item): ?>
I tried it into the function too but without success.
Any kind of idea for me?
You need to filter first, then limit, otherwise it doesnβt make sense.
On a side note: Your code would be more readable, if you store your filtered collection in a variable first, then do your loop.
Thank you,
For a better understanding, because Iβm not so familiar with PHP, here is a cleaned example which is not running:
<?php foreach (page('concert')->children()->listed() as $item):
$item = $item->filterBy('date', 'date >=', time())->limit(5);
?>
<h2><?= $item->title() ?></h2>
<?php endforeach ?>
You cannot filter a single item.
<?php
$items = page('concert')->children()->listed()->filter(fn ($child) => $child->date()->toDate() < time())->limit(5);
foreach ($items as $concert_item): ?>
1 Like
Thank you very much,
My mistake. I thought the complete data source is available and the single item will be managed by Kirby in the background. But now it is clear.