gpt4 book ai didi

Laravel Collection() 与 collect()

转载 作者:行者123 更新时间:2023-12-04 10:55:22 29 4
gpt4 key购买 nike

我有一系列存储估计信息的数据库表。当设置某些边界时,我试图从所有数据库表中返回所有数据。

收藏

        $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() );

enter image description here

和收集()
$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(),
]);

enter image description here

对于此示例,只有 4 个数据库表具有与此“估计”相关的数据。理想情况下,应该有 1 个或全部有数据。

为什么 Collection 最多只返回 3 个,而即使它们是空的,collect 也会返回全部?

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

这是我正在使用的查询。
    private function getBoxVent() {
return ProposalBoxVent::where('proposalRecordID', $this->getProposalAPI())->get();
}

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

如果我使用 concat而不是 merge我得到了想要的结果。
enter image description here

最佳答案

我相信您在使用 Laravel 中的合并方法时遇到了问题。 , 如果它是关联数组或索引数组,合并的工作方式会有所不同。如果它被索引,它应该像 concat 一样工作。如果它是关联数组,它会尝试按键值基础合并它。因此,我相信您的一种基础方法会错误地返回某些内容。

话虽如此,如果您正在使用索引数组 concat 更好更快,因为它不必检查。如果要合并 ['apple' => 'green']['banana' => 'yellow']进入 ['apple' => 'green', 'banana' => 'yellow']你应该使用合并。

关于您的问题,请使用 concat相反,如果您正在使用索引数组。合并在大集合中也可能有糟糕的表现。

$estimateItems = $estimateItems->concat($this->getBoxVent());

关于Laravel Collection() 与 collect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240347/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com