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

amk's avatar
Level 4

Property [id] does not exist on this collection instance

Hello,I want to crate api with laravel,But my code show error like that.. Property [id] does not exist on this collection instance this is from my controller...

   public function read(Request $request){

        return ClassDetailResource::collection(ClassDetail::where('course_id',$request->id)->with('classes')->with('teach')->get());
    }

this is ClassDetail model..

class ClassDetail extends Model
{
    public function classes(){
        return $this->hasMany('App\Class_');
    }

    public function teacher(){
        return $this->belongsTo('App\Teacher');
    }

    public function teach(){
        return $this->teacher()->select('id','name');
    }
}

this is classdetail resource...

public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'name'=>$this->name,
            'count'=>count($this->classes),
            'course_id'=>$this->course_id,
            'classes'=>new ClassResource($this->whenLoaded('classes')->slice(0,3)),

        ];
    }

this is Class Resource..

public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'img_url'=>$this->img_url,
            'status'=>$this->status,
            'title'=>$this->title,
            'description'=>$this->description,
            'price'=>$this->price,
            'teacher_id'=>new TeacherResource($this->whenLoaded('teach')),
            'created_at'=>$this->created_at
        ];
    }

it show error like that..

    "message": "Property [id] does not exist on this collection instance.",
    "exception": "Exception",
    "file": "/home/aung/Desktop/conceptX/vendor/laravel/framework/src/Illuminate/Support/Collection.php",
    "line": 1879,
0 likes
13 replies
tykus's avatar

get() will always return a Collection, use first() instead to return a single instance

 return ClassDetailResource::collection(
    ClassDetail::where('course_id',$request->id)->with('classes')->with('teach')->first()
);
10 likes
amk's avatar
Level 4

can't work sir @tykus .. show error like that

    "message": "Method Illuminate\Database\Query\Builder::mapInto does not exist.",
    "exception": "BadMethodCallException",
    "file": "/home/aung/Desktop/conceptX/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
    "line": 2575,
maslauskast's avatar

Seems like you're trying to get the ID from collection instance instead of from an item IN that collection:

collect($classes)->id; // error
collect($classes)->first()->id; // no error

If you could provide the full error stacktrace, it would be easier to pinpoint the exact spot where this error occurs.

maslauskast's avatar

Are you trying to take only one ClassDetail object or a collection of them?

1 like
tykus's avatar

Your teach method is likely the issue here.; can you try the following instead:

return ClassDetailResource::collection(
    ClassDetail::where('course_id',$request->id)->with('classes')->with('teacher:id,name')->first()
);
1 like
deansatch's avatar

in your read() method dd($request) and see if that has an id. If it does make sure that ClassDetail actually has a row with course_id equal to that

1 like
amk's avatar
Level 4

now,I'm trying without teacher

        return ClassDetailResource::collection(ClassDetail::where('course_id',$request->id)->with('classes')->first());

but still showing error like that.. @tykus

"message": "Method Illuminate\Database\Query\Builder::mapInto does not exist.",
    "exception": "BadMethodCallException",
    "file": "/home/aung/Desktop/conceptX/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
    "line": 2575,
amk's avatar
Level 4

if I don't use new Class Resource in classdetail resource,it can work well. I change code from...

public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'name'=>$this->name,
            'count'=>count($this->classes),
            'course_id'=>$this->course_id,
            'classes'=>new ClassResource($this->whenLoaded('classes')->slice(0,3)),

        ];
    }

To..

    public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'name'=>$this->name,
            'count'=>count($this->classes),
            'course_id'=>$this->course_id,
            'classes'=>$this->classes->slice(0,3),

        ];
    }

why can't use Class Resourece in ClassDetail Resource? but I need to use teacher.

maslauskast's avatar
Level 8

Because it's a one-to-many relationship.

Using this new ClassResource($this->whenLoaded('classes')->slice(0,3)) expects 'classes' relationship to be one-to-one or many-to-one, you should use this:

'classes' => ClassResource::collection($this->whenLoaded('classes')->slice(0,3)),
6 likes
jamesbuch79's avatar

In my case I was using a collection in my API when it expected a model instance. The collection does not have the id property, the model instance does.

Collection is a bag-o-stuff, not containing the properties and methods of a model class instance like id, name, email, etc., like a User model might have. Instead it has a lot of its own useful methods that work on collections of models (or other things).

So I had a query like Property::where('id', ... some way of selecting a random id which exists ->pluck('id')->toArray() )->get() which returned a collection instead of the $property instance I was expecting with the ->id field available.

I did $property = Property::inRandomOrder()->first() instead to get a collection of Property, and first() on that to get the actual model instance of a randomish Property, which then had the id property on it, so I could use $property->id.

Please or to participate in this conversation.