I have a big collections of authors with this structure:
- authors (id, profile_id, title, name) -> this are 590 authors
I have also 4 collections, with author.id == author_id
- sales (id, author_id, salesTotal)
- subscribers (id, author_id, subscribersTotal)
- futureEvents (id, author_id, scheduledTotal)
- reservations (id, authors_id, reservationsTotal)
Not all authors have sales, subscribers, futureEvents and/or reservations. The collections are not organized in any particular order.
In order to build the authors Index view, I am making a FOREACH loop through all authors, so I need to join all those five collections into one like this:
- authors (id, profile_id, title, name, sales, subscribers, futureEvents, reservations)
How could I joint all those collections based in the author_id, having in mindt that each collection has different lenght and variate dinamically (new authors could sign in, new sales, subscribers sign in or out, etc) from day to day. So, the index can show different values from day to day
Any idea how?
EDIT: I am using LARAVEL, those collections are the result of each of a different query. For example:
$authorsSubscribers = DB::table('authors')
->join('subscriptions', 'subscriptions.author_id', '=', 'authors.id')
->groupBy('authors.title')
->selectRaw('authors.id, COUNT(subscriptions.author_id) AS Subscribers')
->get();
As result, my collections have all similar structure, like this one:
Collection {#86071 ▼
#items: array:5 [▼
0 => {#86015 ▼
+"id": 16
+"Subscribers": 269
}
1 => {#86016 ▼
+"id": 49
+"Subscribers": 269
}
2 => {#86017 ▼
+"id": 20
+"Subscribers": 269
}
3 => {#86018 ▼
+"id": 10
+"Subscribers": 269
}
4 => {#86019 ▼
+"id": 11
+"Subscribers": 269
}
]
}
I can not make just one big giant query, because I am grouping (GroupBy, counting (COUNT), adding (SUM) and so on.