Laravel Collection() 与收集()
Laravel Collection() vs collect()
我有一系列存储估算信息的数据库表。当设置了某些周界时,我正在尝试 return 来自所有数据库表的所有数据。
Collection
$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() );
和收集()
$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 个数据库表具有与此 "estimate" 相关的数据。理想情况下会有 1 个或全部有数据。
为什么 Collection 只有 return 最多 3 个,即使它们是空的也要收集 return 全部?
当数据库中有 4 个或更多时,我只有 $estimateItems = new Collection();
return 3 是不是做错了什么?
这是我正在使用的查询。
private function getBoxVent() {
return ProposalBoxVent::where('proposalRecordID', $this->getProposalAPI())->get();
}
private function getCoatings() {
return ProposalCoatings::where('proposalRecordID', $this->getProposalAPI())->get();
}
如果我使用 concat
而不是 merge
,我会得到想要的结果。
您传递给 collect()
的是您想要作为集合的 'items' 的数组。你是说你想要一个包含 18 个项目的集合;只是碰巧大多数都是空的,但这对数组或集合来说无关紧要。
合并时,您实际上是将集合中的当前项与一组新项合并。合并时,某些东西可能会覆盖其他东西而不是添加到集合中。类似于 array_merge
的工作方式,因为这是内部发生的事情。 Eloquent 集合由 'id' 键控。
也可以尝试使用 values
在合并前重置密钥:
$collection->merge($this->getValleyMetal()->values());
我相信您在使用 Laravel
中的合并方法时遇到问题,如果它是关联数组或索引数组,合并的工作方式将有所不同。如果它被编入索引,它应该像 concat 一样工作。你如果它是关联数组,它将尝试通过键值基础合并它。因此,我认为您的一种基本方法 return 是错误的。
话虽如此,如果您使用的是索引数组,concat 会更好更快,因为它不需要检查。如果你想将 ['apple' => 'green']
和 ['banana' => 'yellow']
合并到 ['apple' => 'green', 'banana' => 'yellow']
中,你应该使用 merge.
关于您的问题,如果您使用的是索引数组,请改用 concat
。合并在大集合中也可能表现不佳。
$estimateItems = $estimateItems->concat($this->getBoxVent());
我有一系列存储估算信息的数据库表。当设置了某些周界时,我正在尝试 return 来自所有数据库表的所有数据。
Collection
$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() );
和收集()
$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 个数据库表具有与此 "estimate" 相关的数据。理想情况下会有 1 个或全部有数据。
为什么 Collection 只有 return 最多 3 个,即使它们是空的也要收集 return 全部?
当数据库中有 4 个或更多时,我只有 $estimateItems = new Collection();
return 3 是不是做错了什么?
这是我正在使用的查询。
private function getBoxVent() {
return ProposalBoxVent::where('proposalRecordID', $this->getProposalAPI())->get();
}
private function getCoatings() {
return ProposalCoatings::where('proposalRecordID', $this->getProposalAPI())->get();
}
如果我使用 concat
而不是 merge
,我会得到想要的结果。
您传递给 collect()
的是您想要作为集合的 'items' 的数组。你是说你想要一个包含 18 个项目的集合;只是碰巧大多数都是空的,但这对数组或集合来说无关紧要。
合并时,您实际上是将集合中的当前项与一组新项合并。合并时,某些东西可能会覆盖其他东西而不是添加到集合中。类似于 array_merge
的工作方式,因为这是内部发生的事情。 Eloquent 集合由 'id' 键控。
也可以尝试使用 values
在合并前重置密钥:
$collection->merge($this->getValleyMetal()->values());
我相信您在使用 Laravel
中的合并方法时遇到问题,如果它是关联数组或索引数组,合并的工作方式将有所不同。如果它被编入索引,它应该像 concat 一样工作。你如果它是关联数组,它将尝试通过键值基础合并它。因此,我认为您的一种基本方法 return 是错误的。
话虽如此,如果您使用的是索引数组,concat 会更好更快,因为它不需要检查。如果你想将 ['apple' => 'green']
和 ['banana' => 'yellow']
合并到 ['apple' => 'green', 'banana' => 'yellow']
中,你应该使用 merge.
关于您的问题,如果您使用的是索引数组,请改用 concat
。合并在大集合中也可能表现不佳。
$estimateItems = $estimateItems->concat($this->getBoxVent());