Function groupBy not working after update from kirby 2

I have projects with awards in a structure field.
I want to have a list of all awards grouped by their date.

In Kirby 2 this worked:

Blueprint project.yml

title: Projekt
fields:
  ...
  awards:
    label: Auszeichnungen
    type: structure
    fields:
      title:
        label: Auszeichnung
        type: text
        required: true
      date:
        label: Datum
        type: date
        required: true
        format: DD.MM YYYY

controller:

<?php
return function ($site, $pages, $page) {

  $projects_with_awards = page('projekte')
    ->children()
    ->filterBy('template', 'project')
    ->filterBy('awards', '!=', '');

  $tmp = [];
  foreach ($projects_with_awards as $project) {
    foreach ($project->awards()->toStructure() as $award) {
      if($award->date()) {
        $tmp[] = [
          'title'   => $award->title()->value(),
          'date'    => $award->date()->toDate('%Y'),
          'project' => $project,
        ];
      }
    }
  }

  $projects = new Collection($tmp);
  $projects = $projects->sortBy('date','desc')->groupBy('date');

  return compact('projects');
};

now I get this error:

Invalid grouping value for key: 4

Any ideas how to fix this?

Here you have to make sure that the date is not empty:

 if($award->date()->isNotEmpty())

Your condition, however, is always true, because $award->date() returns a field object.

thank you for spotting that :grinning: