'download_url' => isset($track->download_url) ? $track->download_url : null;
or
'download_url' => null;
if (isset($track->download_url))
{
'download_url' => $track->download_url;
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello all,
I'm currently fetching tracks from SoundCloud's API and storing some details in my database.
Sometimes, a track is not available for download - therefore when attempting to access a property from the response, it will throw an error regarding an undefined property.
In this case, the download_url.
Here's my code;
$soundcloud = new SoundCloud(
getenv('SC_CLIENT_ID'),
getenv('SC_CLIENT_SECRET')
);
$tracks = json_decode($soundcloud->get('/users/{myUserId}/favorites')->request()->bodyRaw());
foreach($tracks as $track) {
$artist = Artist::create([
'sc_id' => $track->user->id,
'name' => $track->user->username,
'permalink_url' => $track->user->permalink_url,
'avatar_url' => $track->user->avatar_url
]);
Track::create([
'sc_id' => $track->id,
'artist_id' => $artist->id,
'title' => $track->title,
'duration' => $track->duration,
'stream_url' => $track->stream_url,
'waveform_url' => $track->waveform_url,
'download_url' => $track->download_url, // Here is my problem
'permalink_url' => $track->permalink_url,
'artwork_url' => $track->artwork_url
]);
}
Without writing an if statement and duplicating my code, I was wondering if it was possible to check if this property exists on the fly? If it doesn't return null
I originally attempting something like this;
'download_url' => $track->download_url ?: null,
But that doesn't work.
Any help would be greatly appreciated.
'download_url' => isset($track->download_url) ? $track->download_url : null;
or
'download_url' => null;
if (isset($track->download_url))
{
'download_url' => $track->download_url;
}
Please or to participate in this conversation.