0
如果服務的相關協議過期或接近其結束日期,則將服務添加到集合。這部分工作正常。但是,由於這只是一個部分,我想將問題原因添加到對象中(例如,下一部分將突出顯示硬件問題)。添加到手動添加的集合中的對象的數組屬性
這對第一次迭代是可以的,但是如果一個服務有多個需要記錄的問題,我不能使用setAttribute()
(因爲它只是覆蓋了前一個條目),並且無法正常工作(如add()
或push()
,filter()
)。
圍繞它的代碼是:
$issuesCollection = collect([]);
$agreementsRed = Agreement::with('services')->whereDate('expiry_date', '<', Carbon::today()->toDateString())
->orderBy('vendor', 'asc')
->get();
foreach ($agreementsRed as $agreement) {
foreach ($agreement->services as $service) {
if (!$issuesCollection->contains('service_full_name', $service->service_full_name)) {
$issuesCollection->push($service);
$service->setAttribute('issues', ['Agreement out of date - ' . $agreement->vendor]);
} else {
// add new item to issues array on object in collection
}
}
}
$agreementsAmber = Agreement::with('services')->whereBetween('expiry_date', array(Carbon::today()->toDateString(), $amberDate->toDateString()))
->orderBy('vendor', 'asc')
->get();
foreach ($agreementsAmber as $agreement) {
foreach ($agreement->services as $service) {
if (!$issuesCollection->contains('service_full_name', $service->service_full_name)) {
$issuesCollection->push($service);
$service->setAttribute('issues', ['Agreement nearing end date - ' . $agreement->vendor]);
} else {
// add new item to issues array on object in collection
}
}
}
我怎麼能推到在一個對象只集合中的數組(它不是實際的模型對象)?
你知不知道'的setAttribute(「問題」,$值)'被分配相同的值作爲'$服務 - >問題= $ value'?在循環之前創建局部變量並在循環之後執行賦值,就這些了。 – Chay22
@ Chay22你是什麼意思「相同地賦值爲$ service-> issues = $ value'」?我有一個foreach,我認爲正確顯示'$ service-> issues' - 這只是一個問題。 – kerrin