Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shariff's avatar
Level 50

Property [academic_id] does not exist on this collection instance

Hi

I am using an API resource. I am getting this error in collection instance

  $timetables = TimetableResource::collection(DB::table('time_tables')
        ->whereIn('academic_id', $ids)
        ->whereIn('class', $classid)->get()->groupBy('day'));

and my TimetableResource file

 public function toArray($request)
{
    return [
        'academic_id' => $this->academic_id,
        'day' => $this->day,
        'class' => Course::find($this->class),
        'subject' => Subject::find($this->subject),
        'teacher' => Teacher::find($this->teacher),
        'timeslot' => $this->timeslot

    ];
}
0 likes
7 replies
bobbybouwmann's avatar

Are you sure the id of the table is called academic_id. By default, this field is called id and you can use your resource to rename it if you wish.

Sinnbeck's avatar

You are grouping it by day after getting it from the database. If you remove that groupBy, does the error go away?

bobbybouwmann's avatar

@sinnbeck Aaah good catch! It's not an array of models anymore, but an array of grouped arrays of models. Therefore the first academic_id key doesn't exist in the first array.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

If you absolutely must then this might work

$timetables = DB::table('time_tables')
        ->whereIn('academic_id', $ids)
        ->whereIn('class', $classid)->get()->groupBy('day')->map(function($group) {
            return TimetableResource::collection($group);
});

Please or to participate in this conversation.