动态集合映射:在集合中添加缺失的记录

Dynamic Collection Mapping: Add missing records in collection

提问人:Rakesh kumar 提问时间:2/5/2021 最后编辑:M Khalid JunaidRakesh kumar 更新时间:2/7/2021 访问量:74

问:

我有以下几点

->select(DB::raw('source as Source, customer as Customers, COUNT(*) as count'))
->groupBy('source', 'customer')
->get();

我得到了以下结果

Illuminate\Support\Collection {#460 ▼
  #items: array:4 [▼
    0 => {#466 ▼
      +"Source": "Facebook"
      +"Customer": "Yes"
      +"count": 227
    }
    1 => {#463 ▼
      +"Source": "PinInterest"
      +"Customer": "Yes"
      +"count": 370
    }
    2 => {#465 ▼
      +"Source": "PinInterest"
      +"Customer": "No"
      +"count": 133
    }
    3 => {#467 ▼
      +"Source": "Whatsapp"
      +"Customer": "No"
      +"count": 254
    }
  ]
}

现在,根据客户的不同,Source 可以是 1 - 10 个不同的渠道。 现在客户是“是”或“否”。

如何添加修改集合以添加源 Facebook、客户编号和计数 0,以及源 WhatsApp、客户是和计数 0

来源 : Facebook 客户 : 是 总数 : 227

来源 : Facebook 客户 : 否 计数 : 0

来源 : Whatsapp 客户 : 否 计数 : 254

来源 : Whatsapp 客户 : 是 计数 : 0

Laravel Eloquent Collection-精选

评论

0赞 bhucho 2/5/2021
这是什么意思Finally I want the collection to be like [ Facebook "No" doesn't Exits and Whatsapp "Yes" doesn't Exists]
0赞 Rakesh kumar 2/5/2021
对不起,我更正了。表中没有任何 Facebook 行。此外,表中有一行用于 WhatsApp 客户,但名称不存在,因此计数为 0。
0赞 M Khalid Junaid 2/5/2021
客户是否只有 2 个选项“是”和“否”?或者可以有很多
1赞 Rakesh kumar 2/5/2021
@MKhalidJunaid,只有“是”和“否”。
0赞 bhucho 2/5/2021
如果你只想对你知道其值的属性这样做,那么只使用集合中的transform()怎么样,

答:

1赞 M Khalid Junaid 2/6/2021 #1

使用 laravel 的集合助手,您可以将原始集合中缺失的数据添加为

转型

/** Result from original query */
$collection = collect([
    ["Source"=>"Facebook","Customer"=> "Yes","count"=> 227],
    ["Source"=>"PinInterest","Customer"=> "Yes","count"=> 370],
    ["Source"=>"PinInterest","Customer"=>"No","count"=> 133],
    ["Source"=>"Whatsapp","Customer"=> "No","count"=>254]
  ]);

/** Unique list of sources */
$sources = $collection->pluck('Source')->unique();

/** Unique list of customers */
$customers = collect(["Yes","No","May Be"]);

$sources->each(function ($source, $sourceKey) use (&$collection,$customers) {
    if($collection->where('Source', $source)->count() < count($customers)){
        $customers->each(function ($customer, $customerKey) use (&$collection,$source) {
            if($collection->where('Source', $source)->where('Customer', $customer)->count() === 0){
                $collection = $collection->merge([["Source"=>$source,"Customer"=> $customer,"count"=>0]]);                
            }
        });
    }
});

/** Sort and print */
echo "<pre>";
print_r($collection->sortBy('Source')->toArray());
echo "</pre>";

输出

Array
(
    [0] => Array
        (
            [Source] => Facebook
            [Customer] => Yes
            [count] => 227
        )

    [4] => Array
        (
            [Source] => Facebook
            [Customer] => No
            [count] => 0
        )

    [5] => Array
        (
            [Source] => Facebook
            [Customer] => May Be
            [count] => 0
        )

    [1] => Array
        (
            [Source] => PinInterest
            [Customer] => Yes
            [count] => 370
        )

    [2] => Array
        (
            [Source] => PinInterest
            [Customer] => No
            [count] => 133
        )

    [6] => Array
        (
            [Source] => PinInterest
            [Customer] => May Be
            [count] => 0
        )

    [3] => Array
        (
            [Source] => Whatsapp
            [Customer] => No
            [count] => 254
        )

    [7] => Array
        (
            [Source] => Whatsapp
            [Customer] => Yes
            [count] => 0
        )

    [8] => Array
        (
            [Source] => Whatsapp
            [Customer] => May Be
            [count] => 0
        )

)