【发布时间】:2021-12-30 20:18:04
【问题描述】:
union() 有问题,无法插入我的新集合
在我的每个我都有 $mean 集合与女巫我搜索我测试的 slices() 并有一些 $_availabaleSlices 插入到我的 $slices 中(id:4 然后 id:7) 但是在整个代码通过后,我在 $slices 中只有第一个 $_availabaleSlices。 我做错了什么?
代码
$slices = new Collection();
Means::whereIn('id', $meansId)
->get()
->each(function ($mean) use (&$slices, $weight) {
$_availabaleSlices = $mean->slices()
->betweenWeight($weight)
->get();
if (0 < $_availabaleSlices->count()) {
dump('NEEDED',$_availabaleSlices->toArray());
dump('BEFORE',$slices->toArray());
$slices = $slices->union($_availabaleSlices);
dump('AFTER',$slices->toArray());
}
});
dd('RESULTS', $slices->toArray());
结果
"NEEDED"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"BEFORE"
[]
"AFTER"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"NEEDED"
array:1 [
0 => array:5 [
"id" => 7
"deliveries_mean_id" => 4
"weight_min" => 150.0
"weight_max" => 700.0
"deleted_at" => null
]
]
"BEFORE"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"AFTER"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"RESULTS"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
【问题讨论】:
-
Union其实是基于key进行的。
标签: laravel laravel-8 laravel-collection