【问题标题】:Laravel - Collection merge() returning a single rowLaravel - 集合合并()返回单行
【发布时间】:2018-06-20 21:07:55
【问题描述】:

我对模型集合 (Eloquent) 有疑问。

我有两个模型 ActivationDetail 的集合,我需要将它们合并到一个集合中,集合具有相同的结构,所以合并它们不会很困难吧?

好吧,在互联网和 API 本身中搜索我发现了 merge() 方法,它允许我将两个集合合二为一。喜欢找到herehereand here

但是,此方法返回我一行。 (sizeof($detail) 正在返回 1)虽然格式正确,但我不知道我在这里缺少什么。

我做错了什么?

//first collection
$detail = ActivationDetail::[redacted]->get();

// second collection, the query is of the same structure 
// as the previous one, but it's edited to allow the selection 
// of null fields (which can't be possible in the previous 
// due how Inner Joins work)
$detailB = ActivationDetail::[redacted]->get();

$detail = $detail->merge($detailB);

//this returns "1"
dd(sizeof($detail));

【问题讨论】:

  • 请显示dd($detail, $detailB)的结果

标签: php laravel eloquent laravel-collection


【解决方案1】:

如果您正在运行两个在结果集中具有相同列的查询,您可能需要查看unions。这会将两个查询合并到一个查询中,为您合并结果。

$query1 = ActivationDetail::[redacted];
$detail = ActivationDetail::[redacted]->union($query1)->get();

【讨论】:

  • 完美运行!谢谢!
猜你喜欢
  • 2019-04-03
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 2018-11-11
  • 2020-09-04
  • 2019-02-01
相关资源
最近更新 更多