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

henryoladj's avatar

Property [id] does not exist on this collection instance with my API (I have been stuck for months)

I am getting this error Property [id] does not exist on this collection instance when trying to build my API.

Album Model

<?php

namespace App;

use App\Track;

use Illuminate\Database\Eloquent\Model;

class Album extends Model
{
    public function artist()
    {
        return $this->belongsTo(Artist::class);
    }

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

    public function tracks()
    {
        return $this->hasMany('App\Track');
    }

}



Track Model

<?php

namespace App;

use App\Album;

use Illuminate\Database\Eloquent\Model;

class Track extends Model
{
    public function artist()
    {
        return $this->belongsTo('App\Artist');
    }
    
    public function albums()
    {
        return $this->belongsTo('App\Album');
    }
}

Album Resource

    public function toArray($request)
    {

        return [
            'albumDetails' => [   
            'id' => $this->id,
            'title' => $this->title,
            'artworkPath' => $this->artwork_path,
            'albumdate' => $this->album_date,
            'upc' => $this->upc,
            'recordlabel' => $this->record_label,
            ],
            'artistId' => [
                'id' => $this->artist->id,
                'artistName' => $this->artist->name,
            ],
            'artistName' => [
                'id' => $this->artist->id,
                'artistName' => $this->artist->name,
            ],
            'genre' => [
                'id' => $this->genre->id,
                'genre' => $this->genre->name,
            ],
            'track' => [
                'id' => $this->track->id,
            ],
        ];
    }

Album Controller

public function show($id)
    {
        // Get single album
        $album = Album::findOrFail($id);

        // Return single album as a resource
        return new AlbumResource($album);
    }

Route

// List albums
Route::get('albums', 'AlbumController@index');

// List single album
Route::get('album/{id}', 'AlbumController@show');

Please i really need help, i have been stuck on this for months.

0 likes
10 replies
tykus's avatar

Which endpoint are you visiting whenever you see that error?

You have a tracks relation, so what is the track referred to in the Album resource?

Snapey's avatar

where does $this->track come from?

Nakov's avatar

@henryoladj the error is self explanatory, you have probably multiple tracks in an album, which makes sense.. So trying to call $this->track->id throws the error, try comment that part out, and check if it works. If it does then it means that you need to loop through the tracks and add each one to the album. Or create a Track resource.

tykus's avatar

If the relation is tracks, then what is $this->track (in the Resource)?

I suspect you have a typo in either your code or your question. In any every, somewhere you are working with a Collection

henryoladj's avatar

@snapey i am trying to get list of tracks related to the album, what am i doing wrong?

tykus's avatar
tykus
Best Answer
Level 104

Ok, so the relation is tracks, you need to use this in the Album resource:

    public function toArray($request)
    {

        return [
            'albumDetails' => [   
            'id' => $this->id,
            'title' => $this->title,
            'artworkPath' => $this->artwork_path,
            'albumdate' => $this->album_date,
            'upc' => $this->upc,
            'recordlabel' => $this->record_label,
            ],
            'artistId' => [
                'id' => $this->artist->id,
                'artistName' => $this->artist->name,
            ],
            'artistName' => [
                'id' => $this->artist->id,
                'artistName' => $this->artist->name,
            ],
            'genre' => [
                'id' => $this->genre->id,
                'genre' => $this->genre->name,
            ],
            'tracks' =>$this->tracks, // change is here
        ];
    }

This will get the array representation of the tracks collection as a property of the Album resource

1 like
henryoladj's avatar

@tykus i am trying to get list of tracks related to the album, what am i doing wrong exactly because i am still stuck?

Please or to participate in this conversation.