Users method not working

i have a little user methof that goes over all uses with a certain role and returns some data from a stucture field in each users account… atleast that is what i want it to do!

I dont understand why this doesnt work…

    'userMethods' => [
        'getSubmissions' => function ($module) {
   
            $coachSubmission = kirby()->users()->role('coach');
            $submission = [];
          

            foreach ($coachSubmission as $item) {
             
              $data = $item->homework()->toStructure();
           
                $submission[] = [
                  'unit' => $data->unitname(),
                  'pagesubmitted' => $data->pageuuid(),
                  'submission' => $data->submission(),
                  'grade' => 'fail'
  
                
                ];

            };
            return $submission;
        }
    ],

keeps telling the structure cant be resolved. I know the data is there, i can see it if I return $item just insaide the for each loop.

What am i missing? can i not use toStructure here?

What I don’t understand why this is a user method, shouldn’t it be a users method instead, given that you try to fetch data for multiple users, i.e. all with the role coach?

Oh yeah that was my mistake, mistyped it. Either way, it still doesnt work as a users method.

The other problem is that you either need to loop through the structure items, or fetch them by index.

	$coachSubmission = kirby()->users()->role('coach');
			$submission = [];
			
			
			foreach ($coachSubmission as $item) {
				$data = $item->homework()->toStructure();
				foreach ($data as $homework) {
					
					
					$submission[] = [
						'unit'          => $homework->unitname()->value(), // also note that we want the value here, not a field object
						'pagesubmitted' => $homework->pageuuid()->value(),
						'submission'    => $homework->submission()->value(),
						'grade'         => 'fail'
					
					
					];
				}
				
			}
			return $submission;

Ahhh silly me, of course! Been staring at this project for hours… probably natures way of telling me to take a break. Thanks @texnixe

I’d also like to add that it would make sense not to redefine the users this applies to, but define the $users object on which you call this method has exactly the users you want to apply this to.

$users       = $kirby->users()->role('coach');
$submissions = $users->getSubmissions();

In your method:

'getSubmissions' => function ($module) {
    $coaches = $this;
    foreach ($coaches as $item) {
      // ...
    }
}

}