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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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
Please or to participate in this conversation.