每次不工作期间的 Laravel 收集联合

Posted

技术标签:

【中文标题】每次不工作期间的 Laravel 收集联合【英文标题】:Laravel collection union during each not working 【发布时间】: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进行的。 【参考方案1】:

找到了解决办法。我需要使用-&gt;merge() 而不是-&gt;union()

【讨论】:

以上是关于每次不工作期间的 Laravel 收集联合的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 路由问题:自动重定向到根文件夹

laravel cookie加密解密原理

公共 Laravel API 的身份验证

laravel-mix 文档翻译

如何从 Windows 命令提示符在 Laravel 中运行 PHPUnit

[MRCTF2021]ez_laravel复现