Problem with images()->not(...)

A user has the ability to select a cover image for a project. If they don’t choose one, the first image found is used.

In the template, the cover image is displayed, followed by a description, then, a gallery with the rest of the images.

I’m getting a warning with this: Warning: Illegal offset type in unset in /Users/Joseph/Sites/tkelly/kirby/toolkit/lib/collection.php on line 166

// site/models/project.php 
<?php

class ProjectPage extends Page {

  public function get_cover() {
    $image_str = $this->cover_image();
    if (null !== $this->files()->find($image_str)) {
      return $this->files()->find($image_str);
    } elseif ($this->images()->count() > 0) {
      return $this->images()->first();
    } else {
      return site()->pages()->find('error')->files()->find('blank.jpg');
    }
  }

  public function gallery() {
    $cover = $this->get_cover();
    return parent::images()->not($cover)->sortBy('sort', 'ASC');
  }
}

Am I doing something wrong here? Or is this a bug?

Could be that the not() method expects a string and you are passing it a file object?

Yes, that is it. The current version does not accept file objects, but the next Kirby version will. So your code should work soon.

Until then:

return parent::images()->not(strtolower($cover->filename()))->sortBy('sort', 'ASC');
1 Like