It’s probably very easy but I’m struggling figuring out how to do this.
I want to apply sortBy
only if the URL is /?filter=newest
.
<?php
$filterBy = get('filter');
$items = $site
->find('mypage')
->children()
->listed()
->when($filterBy, function($filterBy) {
return $this->filterBy('category', $filterBy, ',');
})
->sortBy('date', 'desc')
->paginate(3);
$pagination = $items->pagination();
?>
Thanks in advance for helping 
If I got it right, you can use sortBy()
in when
condition.
<?php
$filterBy = get('filter');
$items = $site
->find('mypage')
->children()
->listed()
->when($filterBy, function($filterBy) {
return $this
->filterBy('category', $filterBy, ',')
->sortBy('date', 'desc');
})
->paginate(3);
$pagination = $items->pagination();
?>
We need a condition here in addition to what @ahmetbora posted:
<?php
$filterBy = get('filter');
$items = $site
->find('mypage')
->children()
->listed()
->when($filterBy, function($filterBy) {
$filtered = $this->filterBy('category', $filterBy, ',')
if ( $filterBy === 'newest' ) {
$filtered = $filtered->->sortBy('date', 'desc');
}
return $filtered;
})
->paginate(3);
$pagination = $items->pagination();
1 Like