将现有返回的数组附加到现有数组

问题描述:

Working with a database that has a table and fields such as

album

id
titleId
titleName

track

id
titleId
trackId
trackName

artist

id
artistId

track_log

id
artistId
titleId

in this case mutiple artists can own a track, and there can be multiple tracks on an album.

Using a model I am able to return all tracks for an album like so

public function track() {
    return $this->belongsTo('App\Album', 'titleId', 'titleId');
}

So I am now able to use $album->track to pull all tracks on that album.

It would return

{
"id": 1,
"titleId": 3,
"titleName": "Why so hard?",
},

I am looking for a way to highlight the tracks on the album that artist worked on by appending the artistId on to the results for the track in the model? Something along the lines as

trackHighlightArtist()

So that that would return something such as:

{
"id": 1,
"titleId": 3,
"titleName": "Why so hard?",
"artistId": 4,
},

Is this possible?

I can see from your previous reply that you have a way of getting highlighted tracks. If you dont, create a relationship to get if then you can push it into your collection using $collection->push(['key' => 'value']) pushing into laravel collections