0

I'm trying to get the data from my Lists table and include an array with the ID's of the tracks in that list.

This is a sample of the database model, with a relation N:M.

database model

In my List model, I added this method:

public function tracks()
{
   return $this->belongsToMany(Track::class, 'List_Tracks', 'id_list', 'id_track');
}

So, in my ListController, I'm doing the following:

$list = List::find($id);
$list->tracks_list = $list->tracks->pluck('track_id');
return $list;

And what I get is as many objects as tracks I have in a same list, for example:

[
{
 "id_track": 1,
 "name": "Yesterday",
 "tracks_list": [
  1,
  2
 ]
 "pivot": {
    "id_list": 1,
    "id_track": 1
 }
},
{
 "id_track": 2,
 "name": "Lucy in the sky with diamonds",
 "pivot": {
    "id_list": 1,
    "id_track": 2
 }
}
]

But what I want to get is:

{
 "id_list": 1,
 "name": "The Best of The Beatles",
 "tracks_list": [
  1,
  2
]
}

I think the things I've tried are much more complex than the proper solution.

How would you get the data in this way?

Thanks in advance.

yisus
  • 125
  • 1
  • 7
  • It' seems duplicate of https://stackoverflow.com/questions/27434338/laravel-get-pivot-data-for-specific-many-to-many-relation ? – Vikash Pathak May 25 '19 at 10:08
  • Possible duplicate of [Laravel: Get pivot data for specific many to many relation](https://stackoverflow.com/questions/27434338/laravel-get-pivot-data-for-specific-many-to-many-relation) – Vikash Pathak May 25 '19 at 10:10
  • 1
    have you tried `List::find($id)->with('tracks');`? I think that should take data from the relation instead of pivot table. You will might need to change the call a bit = `List::with('tracks')->where('id', '=', $id)->first()`. – Silencesys May 25 '19 at 10:12
  • @Silencesys That's correct, and it brings the whole object, what makes me think that perhaps I just need the ID's within an array, something like: { "id_list": 1, "name": "The Best of The Beatles", "tracks": [ 1, 2 ] } Is there any way in Laravel to do this easily? – yisus May 27 '19 at 11:31

1 Answers1

0

You need to load the relationship first and then write rest of the eloquent query, otherwise you will get only list of ids from the pivot table.

List::with('tracks')->where('id', '=', $id)->first();
// Following method should work too
List::with('tracks')->find($id);


Based on your commentary - to get only array of ids related to the list you don't need to load the relationship and can use only:

List::find($id)->tracks()->pluck('id');
// In case previous method will not work, you can try this one:
List::find($id)->tracks()->pluck('tracks.id');
// before dot should be name of the table, not relationship's.

So if you need only ids of the tracks that are attached to the playlist. I would recommend adding to your List model following method:

// App\Models\List
public function getTrackIdsAttribute()
{
    return $this->tracks->pluck('id')->toArray();
}

Then you should be able to simply call List::find($id)->trackIds to get all ids of tracks attached to the given list. If you're not sure why I am defining the method as getTrackIdsAttribute and call only trackIds on the model, take a look at Eloquent: Mutators.

Silencesys
  • 545
  • 5
  • 12