The error message "Call to a member function relationLoaded() on array" occurs because the whenLoaded method is called on an array instead of a model. To fix this, you can create a new instance of the model and pass the array to it. Here's an example:
$exams = [
['id' => 12345, 'date' => '2015-01-28'],
];
$examModels = collect($exams)->map(function ($exam) {
return new Exam($exam);
});
return ExamResource::collection($examModels);
In this example, we create a new collection of Exam models from the array using the map method. Then we pass this collection to the ExamResource instead of the original array. This way, the whenLoaded method will work as expected.