If collection cover field has an image/value

I feel like I should be able to do this by now, seeing as I already do things like if bla bla toFile() etc… but I can’t seem to get my head around this one.

My home controller is as follow:


return function ($site) {

    $limit = 5;

    $list = page('blog')->children()->listed()->sortBy('itemDate', 'desc', 'itemTime', 'desc')->limit(5);

    if ($site->find('photography')) {
        $album = page('photography')->children()->listed()->sortBy('itemDate', 'desc', 'itemTime', 'desc')->limit(5);

        $list = $list->add($album)->sortBy('itemDate', 'desc', 'itemTime', 'desc');

    return [
        'list' => $list


I have a field called cover which is obviously an image field and on other pages I will use the if toFile method, but this one I only want to get children that have a cover image.

Is it something like

$list = page(‘blog’)->children()->listed()->sortBy(‘itemDate’, ‘desc’, ‘itemTime’, ‘desc’)->hasImages()->limit(5);

(this doesn’t work btw)

Only a page has an hasImages() method. Here we are dealing with a collection, so you would have to filter by the cover field. This should work:

$list = $list->filter(function($child) {
  return $child->cover()->toFile();

On a side note, if you use the combined list only, it doesn’t really make sense to sort them twice…

Photography may or may not exist so the combined list may never trigger, but I have just changed my return for list to sort and limit.

return [
        'list' => $list->sortBy('itemDate', 'desc', 'itemTime', 'desc')->limit(5)

I will look at the filter - just read the documents, seems to be what I need.