0
I have a playlist table, a playlist_content table and two Vodcontent and Livecontent tables.
Today I make the relationship between vodcontent and livecontent with a hasManyThrough relationship. $this->hasManyThrough(Vodcontent::class, Playlistcontent::class, playlist_id', 'content_id', 'id', 'content_id');
When I list the contents of the playlist, I would like to sort in an order field that is in the intermediate table, playlist_content, only that if I put order by in each relationship (Vod and live), when I merge to display all live content, That’s where it creates the problem, it will pick up in order, but prioritizing through the merge. I thought Collection do a Sort by order, but it does not return the field of this intermediate column, it would have some way to get the column of the intermediate table?
Trying to simplify how it would be visualized. VOD has content in order 1, 3 and Live has content in order 2 With the merge to display the results, if I put Vod as first and live in second, it ends up returning 1,3,2. The correct order would be 1,2,3 in this example.
Relationships in the Playlist model
public function live_contents() {
return $this->hasManyThrough(LiveContent::class, PlaylistContent::class, 'playlist_id', 'content_id', 'id', 'content_id')->orderBy("order");
}
public function vod_contents() {
return $this->hasManyThrough(VODContent::class, PlaylistContent::class, 'playlist_id', 'content_id', 'id', 'content_id')->orderBy("order");
}
For the list of contents with merge
$contents = new Collection($playlist->vod_contents()->paginate());
$livecontents = new Collection($playlist->live_contents()->paginate());
$collection = $contents->merge($livecontents);
return collect($collection);
Your modeling seems strange to me, you have two tables of Contents when you could only have one and indicate a content_type flag. As for this code I see that you have merged into two paginated Collections, the result will not be the two Collections data with a new pagination but the two Paginations together. I may not have read it right.
– Edi. Gomes
Yes, I’m still thinking of improving this modeling, soon joining the vodcontents table and livecontents (which both also have the content type to make another differentiation). Anyway, I think in the place of paginate is get even, maybe I copied from some test.
– braulio_holtz