首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Collection() vs collect()

Laravel Collection() vs collect()
EN

Stack Overflow用户
提问于 2019-12-09 06:10:44
回答 2查看 4.5K关注 0票数 3

我有一系列存储评估信息的数据库表。当设置了某些边界时,我正在尝试返回所有数据库表中的所有数据。

集合

代码语言:javascript
复制
        $estimateItems = new Collection();
    $estimateItems = $estimateItems->merge( $this->getBoxVent() );
    $estimateItems = $estimateItems->merge( $this->getCoatings() );
    $estimateItems = $estimateItems->merge( $this->getCounterFlashing() );
    $estimateItems = $estimateItems->merge( $this->getDripEdge() );
    $estimateItems = $estimateItems->merge( $this->getFasteners() );
    $estimateItems = $estimateItems->merge( $this->getHeadwallFlashing() );
    $estimateItems = $estimateItems->merge( $this->getHipcap() );
    $estimateItems = $estimateItems->merge( $this->getIceShield() );
    $estimateItems = $estimateItems->merge( $this->getPipeFlashing() );
    $estimateItems = $estimateItems->merge( $this->getRidgecap() );
    $estimateItems = $estimateItems->merge( $this->getRidgeVentilation() );
    $estimateItems = $estimateItems->merge( $this->getSheathing() );
    $estimateItems = $estimateItems->merge( $this->getShingles() );
    $estimateItems = $estimateItems->merge( $this->getSidewallFlashing() );
    $estimateItems = $estimateItems->merge( $this->getSkylights() );
    $estimateItems = $estimateItems->merge( $this->getStarterShingle() );
    $estimateItems = $estimateItems->merge( $this->getTearOff() );
    $estimateItems = $estimateItems->merge( $this->getUnderlayment() );
    $estimateItems = $estimateItems->merge( $this->getValleyMetal() );

并收集()

代码语言:javascript
复制
$collectedItems = collect([
        $this->getBoxVent(),
        $this->getCoatings(),
        $this->getCounterFlashing(),
        $this->getDripEdge(),
        $this->getFasteners(),
        $this->getHeadwallFlashing(),
        $this->getHipcap(),
        $this->getIceShield(),
        $this->getPipeFlashing(),
        $this->getRidgecap(),
        $this->getRidgeVentilation(),
        $this->getSheathing(),
        $this->getShingles(),
        $this->getSidewallFlashing(),
        $this->getSkylights(),
        $this->getStarterShingle(),
        $this->getTearOff(),
        $this->getUnderlayment(),
        $this->getValleyMetal(),
    ]);

在这个例子中,只有4个数据库表包含与这个“估计”相关的数据。理想情况下,应该有1个或全部都有数据。

为什么Collection只返回最大值3,而as collect会返回所有值,即使它们为空?

当数据库中有4个或更多时,我是否做错了什么让$estimateItems = new Collection();只返回3?

以下是我正在使用的查询。

代码语言:javascript
复制
    private function getBoxVent() {
    return ProposalBoxVent::where('proposalRecordID', $this->getProposalAPI())->get();
}

private function getCoatings() {
    return ProposalCoatings::where('proposalRecordID', $this->getProposalAPI())->get();
}

如果我使用concat而不是merge,我会得到想要的结果。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-09 06:27:42

我相信你在Laravel中的merge方法有问题,如果merge是一个关联数组或索引数组,那么merge的工作方式会有所不同。如果它被编入了索引,那么它应该像concat一样工作。如果它是关联数组,它将尝试按键值基数合并它。因此,我相信你的一个底层方法错误地返回了一些东西。

也就是说,如果你使用的是索引数组,concat会更好更快,因为它不需要检查。如果你想将带有['banana' => 'yellow']['apple' => 'green']合并到['apple' => 'green', 'banana' => 'yellow']中,你应该使用merge。

对于您的问题,如果您使用的是索引数组,请改用concat。Merge在大集合中的性能也会很差。

代码语言:javascript
复制
$estimateItems = $estimateItems->concat($this->getBoxVent());
票数 2
EN

Stack Overflow用户

发布于 2019-12-09 06:17:21

您传递给collect()的是您想要作为集合的“项”的数组。您说的是想要一个包含18个项的Collection;只是碰巧它们中的大多数都是空的,但这对数组或Collection并不重要。

当您合并时,实际上是将集合的当前项与一组新项合并。合并时,有可能会覆盖其他内容,而不是添加到集合中。与array_merge的工作方式类似,因为这是内部发生的事情。能言善辩的集合通过'id‘作为关键字。

也可以尝试在合并之前使用values重置密钥:

代码语言:javascript
复制
$collection->merge($this->getValleyMetal()->values());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59240347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档