【问题标题】:Dynamic Collection Mapping: Add missing records in collection动态集合映射:在集合中添加缺失的记录
【发布时间】:2021-05-09 00:35:50
【问题描述】:

我有以下

->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 个不同的渠道,具体取决于客户。 现在客户是或否。

如何添加修改集合以添加 Source Facebook、Customer No 和 Count 0,以及 Source WhatsApp、Customer Yes 和 Count 0

来源:脸书 客户:是的 计数:227

来源:脸书 客户:没有 计数:0

来源:Whatsapp 客户:没有 计数:254

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

【问题讨论】:

  • 这是什么意思Finally I want the collection to be like [ Facebook "No" doesn't Exits and Whatsapp "Yes" doesn't Exists]
  • 对不起,我更正了。表中没有 Facebook 的任何行。 Table 也有 WhatsApp Customer 的一行,但名称不存在,所以计数为 0。
  • 客户是否只有两个选项是和否?或者可以有很多
  • @MKhalidJunaid,只有是和否。
  • 如果你只是想为你知道值的属性做这件事,那么只使用集合中的transform()怎么样,

标签: laravel eloquent collection-select


【解决方案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
        )

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多