php - Eloquent 查询数组中的 NULL 结果

标签 php mysql arrays null eloquent

有谁知道为什么这个查询会产生一些 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”循环并转储每个事件、帐户、联系人和委托(delegate)的 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);
}

最佳答案

在您的 map() 回调中,您似乎只为具有联系人的委托(delegate)人返回 foreach 循环的第一次迭代。如果事件没有任何具有联系人的代表,则不会返回任何内容 - 因此为空。这是正确的吗?

正如文档所述:

The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items

因此,您基本上只是将事件集合中的每个项目替换为第一个委托(delegate)的数组结果。如果您想要每个事件的代理人和联系人的完整列表,最好迭代事件和代理人,构建一个单独的数组。像这样的东西可能会起作用:

$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)
        ];
    }
}

关于php - Eloquent 查询数组中的 NULL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33148192/

相关文章:

php - 搜索存储为整数的部分 IP 地址

mysql - 如何计算 MySQL 中给定字符串的匹配数?

php - 通过 PDO 连接 Mysql 时出错,但使用基本 mysqli 命令时出错 SQLSTATE[HY000] [2002]

c++ - 在类模板方法中使用enable_if来检查两个函数参数是否都是std::array或都是std::vector

php - 没有 OOP 的观察者模式逻辑?

php - 使用 AJAX 检查 PHP session 是否存在或过期

php - 如何对右表中的特定行进行左连接?

Ruby - 如果数组中的元素包含某个字符,则返回关联的元素

java - 唯一数字数组

php - 我如何根据另一个查询从 mysql 查询中排除一些记录