有没有人知道为什么这个查询会产生一些NULL结果(数组):
$events = $this->event
->with('delegates', 'course')
->where('start_date', '<=', $today)
->where('active', '1')
->where('event_status_id', '!=', '3')
->hasCosts()
->has('delegates')
->get()
->map(function($event) {
foreach ($delegates = $event->delegates()->has('contact')->get() as $delegate) {
$account = $delegate->contact->account;
return [
'company' => $account->company_name,
'income' => $account->income($delegates),
'profit' => $account->profit($event, $delegates),
'event' => $account->eventDetails($event, $delegates)
];
}
})
->toArray();
我已经进入'foreach'循环并为每个事件,帐户,联系人和委托转储了id,并且那里没有NULL结果。我也没有收到错误。
当我转储$ events变量时,我会收到如下输出:
array(8) {
[0] NULL
[1] array(4) {
["company"] "Razorfish"
["income"] 523
["profit"] "69.29"
["event"] "ITIL® Service Transition Apr 7, 2014 in London (141)"
}
[2] array(4) {
["company"] "European Central Bank - Europa ECB"
["income"] 1332
["profit"] "137.33"
["event"] "ITIL® Service Offerings & Agreements Apr 7, 2014 in London (142)"
}
[3] array(4) {
["company"] "Knowledge Pool - KP delegates"
["income"] 475
["profit"] "-111.75"
["event"] "ITIL® Foundation Apr 7, 2014 in Leeds (143)"
}
[4] array(4) {
["company"] "Plan International/ Plan UK"
["income"] 537
["profit"] "118.43"
["event"] "ITIL® Foundation Apr 14, 2014 in London (144)"
}
[5] array(4) {
["company"] "Cell Therapy Catapult ( part of Guy's hospital)"
["income"] 550
["profit"] "-114.75"
["event"] "ITIL® Service Design Apr 14, 2014 in London (145)"
}
[6] array(4) {
["company"] "European Central Bank - Europa ECB"
["income"] 597
["profit"] "69.80"
["event"] "BCS Specialist Certificate in Supplier Management Apr 14, 2014 in London (146)"
}
[7] array(4) {
["company"] "C Hoare & Co (hoares bank)"
["income"] 523
["profit"] "97.71"
["event"] "ITIL® Continual Service Improvement Apr 23, 2014 in London (148)"
}
}
注意第一个NULL结果。这只是输出的一个示例,但在实际输出中有许多这样的结果。
为简洁起见,初始查询中的hasCosts()方法是查询范围函数:
public function scopeHasCosts($query)
{
return $query->where('tutor_cost', '>', 0)
->orWhere('exam_cost', '>', 0)
->orWhere('material_cost', '>', 0)
->orWhere('venue_cost', '>', 0)
->orWhere('notional_cost', '>', 0)
->orWhere('buy_price', '>', 0);
}
答案 0 :(得分:0)
在map()
回调中,您似乎只返回带有联系人的代理的foreach循环的第一次迭代。如果某个事件没有任何具有联系人的代表,则不会返回任何内容 - 因此为空。这是对的吗?
正如文档所述:
map方法遍历集合并传递每个值 到给定的回调。回调可以自由修改项目和 返回它,从而形成一个新的修改项目集合
因此,您基本上只使用第一个代理的数组结果替换事件集合的每个项目。如果您想要每个事件的联系人的完整列表,那么迭代事件和委托,构建单独的数组可能会更好。这样的事情可能有用:
$events = $this->event
->with('delegates', 'course')
->where('start_date', '<=', $today)
->where('active', '1')
->where('event_status_id', '!=', '3')
->hasCosts()
->has('delegates')
->get();
$results = [];
foreach ($events as $event) {
foreach ($delegates = $events->delegates()->has('contact')->get() as $delegate) {
$account = $delegate->contact->account;
$results[] = [
'company' => $account->company_name,
'income' => $account->income($delegates),
'profit' => $account->profit($event, $delegates),
'event' => $account->eventDetails($event, $delegates)
];
}
}
答案 1 :(得分:0)
正如@benJ所提到的,这部分与没有联系人的代表有关。
每个代表通常都有一个联系人,但如果联系人不知道,他们会链接到一个“未知数”表,该表具有唯一的密钥和与之关联的帐户。但有时候,未知数也没有账号。
我修改了查询以执行以下操作:
$events = $this->event
->has('delegates')
->with('delegates', 'course')
->where('start_date', '<=', $today)
->where('active', 1)
->whereNotIn('event_status_id', [3])
->hasCosts()
->get()
->map(function($event) use ($companies) {
foreach ($delegates = $event->delegates()->has('contact')->get() as $delegate) {
$account = $delegate->contact->account;
return [
'company' => $account->company_name,
'account_manager' => $account->user->name(),
'income' => $account->income($delegates),
'profit' => $account->profit($event, $delegates),
'event' => $account->eventDetails($event, $delegates)
];
}
foreach ($delegates = $event->delegates()->has('unknown')->get() as $delegate) {
$account = $delegate->unknown->account;
if ($account) {
return [
'company' => $account->company_name,
'account_manager' => $account->user->name(),
'income' => $account->income($delegates),
'profit' => $account->profit($event, $delegates),
'event' => $account->eventDetails($event, $delegates)
];
} else {
$costPerDelegate = $event->costs() / count($event->delegates);
$eventVenue = ( ! is_null($event->venue)) ? $event->venue->city : $event->venue_city;
$eventDetails = ($event->course) ? $event->course->title : 'Unknown Course' . ' ' . $event->start_date->toFormattedDateString() . ' in ' . $eventVenue . ' (' . $event->id . ')';
return [
'company' => 'Unknown',
'account_manager' => 'Unknown',
'income' => $delegate->price,
'profit' => number_format((float) $delegate->price - $costPerDelegate, 2, '.', ''),
'event' => $eventDetails
];
}
}
})
->toArray();
现在返回查询的所有结果。如果有人有更清洁的方式这样做,我很乐意听到它。
我现在必须弄清楚如何根据公司名称合并每个结果,然后为每个公司添加收入/利润,以及在事件下创建一个新数组,列出该公司的事件。