Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

laravel - Dynamic Collection Mapping: Add missing records in collection

I have the following

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

I get the follwing results

IlluminateSupportCollection {#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
    }
  ]
}

Now Source can be 1 - 10 different channels depending on the customer. Now Customer Be Yes or No.

How do I add modify collection to add Source Facebook, Customer No and Count 0, and Source WhatsApp, Customer Yes and Count 0

Source : Facebook Customer : Yes Count : 227

Source : Facebook Customer : No Count : 0

Source : Whatsapp Customer : No count : 254

Source : Whatsapp Customer : Yes count : 0

question from:https://stackoverflow.com/questions/66050224/dynamic-collection-mapping-add-missing-records-in-collection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using laravel's collection helpers you can add missing data in your original collection as

Transformation

/** 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>";

Output

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
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...