在 Laravel 中对嵌套结果使用 pluck

using pluck on nested results in Laravel

我有一个查询 returns 以下结果。

Illuminate\Database\Eloquent\Collection {#5145
  all: [
    App\Models\Result {#5207
        id: 198,
        result_id: 30,
        result_type: "App\Models\Touchpoint",
        audit_id: 1,
        weight: 7,
        pics: 0,
        recs: 0,
        rating: 4,
        comments: "none",
        complete: 1,
        created_at: "2022-06-03 03:42:24",updated_at: "2022-06-03 03:42:24",
        result: App\Models\Touchpoint {#5210
            id: 30,
            name: "Lineman",
            description: "The location food offer was available on Lineman",
            sort_order: 25,
            req_pics: 0,
            req_recs: 0,
            sector_id: 1,
            created_at: null,
            updated_at: "2022-04-02 14:02:34",
        },
    },
    App\Models\Result {#5119
        id: 199,
        result_id: 29,
        result_type: "App\Models\Touchpoint",
        audit_id: 1,
        weight: 7,
        pics: 0,
        recs: 0,
        rating: 4,
        comments: "none",
        complete: 1,
        created_at: "2022-06-03 03:43:38",
        updated_at: "2022-06-03 03:43:38",
        result: App\Models\Touchpoint {#5206
            id: 29,
            name: "Grab",
            description: "The location food offer was available on Grab",
            sort_order: 24,
            req_pics: 0,
            req_recs: 0,
            sector_id: 1,
            created_at: null,
            updated_at: "2022-04-02 14:02:26",
        },
    },
  ],
}

这是我用来获取该集合的查询,我希望结果只包含这些结果中的 sort_order、名称、描述、评级和权重,并将它们放在一个数组中。我假设我需要使用 pluck 来获取正确的字段,但是当我尝试 pluck 'result.name' 等时,我被告知结果不存在。

$result = Result::query()->where('audit_id', 1)->where('result_type', 'App\Models\Touchpoint')->whereIn('result_id', $tps)->with('result')->get();

这需要在不操作集合的查询中进行,因为我需要将其输入 Maatwebsite\Excel\Concerns\WithMultipleSheets,这需要查询而不是查询结果。

您可以使用预加载约束

$result = Result::query()
    ->where('audit_id', 1)
    ->where('result_type', 'App\Models\Touchpoint')
    ->whereIn('result_id', $tps)
    ->with('result:id,sort_order,name,description')
    ->select('id', 'result_id', 'rating', 'weight')
    ->get();

如果您想删除嵌套并展平结果集,您可以 map() 覆盖集合

$result->map(function($item) {
    $item['sort_order'] = $item->result->sort_order;
    $item['name'] = $item->result->name;
    $item['description'] = $item->result->description;

    //remove the nested relation
    $item->unsetRelation('result');
})
->toArray();

/**
 * Will return something like
 [
    [
        'id' => 198,
        'result_id' => 30,
        'rating' => 4,
        'weight' => 7,
        'sort_order' => 25,
        'name' => 'Lineman',
        'description' => 'The location food offer was available on Lineman'
    ],
    [
        'id' => 199,
        'result_id' => 29,
        'rating' => 4,
        'weight' => 7,
        'sort_order' => 24,
        'name' => 'Grab',
        'description' => 'The location food offer was available on Grab'
    ]
]
*/

Laravel Docs - Eloquent Relationships - Constraining Eager Loads