【发布时间】:2019-02-15 11:52:08
【问题描述】:
我有大量具有这种结构的作者:
- authors (id, profile_id, title, name) -> this are 590 authors
我也有 4 个收藏,author.id == author_id
- sales (id, author_id, salesTotal)
- subscribers (id, author_id, subscribersTotal)
- futureEvents (id, author_id, scheduledTotal)
- reservations (id, authors_id, reservationsTotal)
并非所有作者都有销售、订阅、futureEvents 和/或预订。这些集合没有按任何特定顺序组织。
为了构建作者索引视图,我正在对所有作者进行 FOREACH 循环,因此我需要将所有这五个集合合并为一个,如下所示:
- authors (id, profile_id, title, name, sales, subscribers, futureEvents, reservations)
我怎样才能联合所有基于 author_id 的集合,记住每个集合都有不同的长度和动态变化(新作者可以登录、新销售、订阅者登录或退出,等)每天。 因此,索引可以每天显示不同的值
知道怎么做吗?
编辑: 我正在使用 LARAVEL,这些集合是每个不同查询的结果。例如:
$authorsSubscribers = DB::table('authors')
->join('subscriptions', 'subscriptions.author_id', '=', 'authors.id')
->groupBy('authors.title')
->selectRaw('authors.id, COUNT(subscriptions.author_id) AS Subscribers')
->get();
因此,我的收藏都有类似的结构,比如这个:
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
}
]
}
我不能只做一个大查询,因为我正在分组(GroupBy,计数(COUNT),添加(SUM)等等。
【问题讨论】:
-
“集合”是指这些对象的数组吗?请分享一些代码。
-
@Jeto 我已经用您要求的信息编辑了我的任务
-
@RafaelMunoz 使用 eloquent 关系,这样你就可以直接获得订阅者、销售、未来事件。
-
@SagarGautam,这将适用于集合,但“未来事件”除外。这里的问题是我需要相关表(模型)中的“WHERE”。据我所知,不可能这样做:WHEREJOIN('date', '>', now()) 或 WHERE('other_table.date', '>', now())。然后我遇到了同样的问题,我需要将该集合加入到以前的集合中