【问题标题】:Joining several collections based in a common id in PHP在 PHP 中加入基于公共 id 的多个集合
【发布时间】: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())。然后我遇到了同样的问题,我需要将该集合加入到以前的集合中

标签: php laravel foreach


【解决方案1】:

我只是添加我找到的解决方案。也许对任何人都有用。

我有trest array:merge、array_map 和array_combine 没有成功。由于数组具有不同的结构、长度和键,因此无法一对一映射。

所以,我只使用了一个 Foreach 循环来“循环”大集合的每个元素,并使用第二个循环来循环每个元素,比较 author_id 是否相等,如果是,则将其附加为属性。

    foreach ($authors as $author  ) {

        foreach ($sales as $sale) {
            if ($author->id === $sale->authorId) {
                $author['salesTotal'] = $sale->salesTotal;
                $author['salesAmount'] = $sale->salesAmount;
            }
        }

        foreach ($futureEvents as $events) {
            if ($author->id === $events->authorId) {
                $author['futureEvents'] = $events->futureEvents;
            }
        }
        foreach ($reservations as $reservation) {
            if ($author->id === $reservation->authorId) {
                $author['reservations'] = $reservation->reservations;
            }
        }
    }

这样我就得到了一个大集合在前端循环。像这样:

Author {#43454 ▼
  #attributes: array:14 [▼
    "id" => 1
    "profile_id" => 4
    "title" => "Missourimacejkovic.giles"
    ... lot of other attributes ...
    "created_at" => "2019-02-09 18:49:16"
    "updated_at" => "2019-02-09 18:49:16"
    "deleted_at" => null
    "subscriptions_count" => 2
    "events_count" => 19
    "salesTotal" => "9086"
    "salesAmount" => 200
    "futureEvents" => 9
    "reservations" => 450
  ]
}

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2020-08-31
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多