Php 为什么foreach循环中的Laravel集合总是拾取最后一个数组?
where子句条件后有12个作业行。因此,我使用foreach语句获得12组属性ID。总共有大约500个属性被分成12组(数量不同)。最后一个数组有40个属性ID。我想将所有500个属性ID都放入$attributesId中,但这段代码只获取最后40个属性ID(使用dd查看)。我认为collect()方法一直在将新集合替换为$attributesId,但我不知道如何累计插入它们。其他一切都可以。请帮忙Php 为什么foreach循环中的Laravel集合总是拾取最后一个数组?,php,laravel,foreach,collections,Php,Laravel,Foreach,Collections,where子句条件后有12个作业行。因此,我使用foreach语句获得12组属性ID。总共有大约500个属性被分成12组(数量不同)。最后一个数组有40个属性ID。我想将所有500个属性ID都放入$attributesId中,但这段代码只获取最后40个属性ID(使用dd查看)。我认为collect()方法一直在将新集合替换为$attributesId,但我不知道如何累计插入它们。其他一切都可以。请帮忙 $jobLines = JobLine::where('job_id', $job->i
$jobLines = JobLine::where('job_id', $job->id)
->whereHas('jobType', function ($q) {
$q->where('code', 'Job_Code');
})
->get();
foreach ($jobLines as $jobLine) {
$jobChildren = JobTypeChild::all();
$attributesId = collect($jobLine->attributes->toArray());
->pluck('job_type_attribute_id');
$this->jmAttributes = JobTypeAttribute::whereIn(
'job_type_attributes.id',
$attributesId
)->join(
...table columns joining
)
->where('job_line_id', $jobLine->id)
->pluck(
'job_line_attributes.value',
'attribute_fields.name'
)
->toArray();
}
dd($attributesId);
因此,首先您要覆盖每个循环中的
$attributesId
变量也是collect($jobLine->attributes->toArray());->>Pull('job_type_attribute_id')代码>偶数有效