Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Conixs's avatar

Group Collection Info

Hello, I'm consuming an API that returns a JSON which I transformed in the below collection:

Collection {#968
  #items: array:2 [
    0 => {#959
      +"IdBoleto": 17406
      +"DueDate": "2019-01-11T00:00:00"
      +"PaymentDate": "2019-01-11T18:44:57.937"
      +"CreditDate": null
      +"PrincipalValue": 4.5
      +"InterestValue": 0.0
      +"FineValue": 0.0
      +"TotalValue": 4.5
      +"PaidValue": 4.5
      +"Status": 5
      +"MailToSend": "[email protected]"
      +"PhoneToSend": null
      +"Comments": "Webhook Teste"
      +"OurNumber": "000000002654"
      +"Identifier": "5"
      +"ReturnMessage": null
      +"ReturnCode": null
      +"Barcode": null
      +"DigitableLine": null
      +"OccurrenceMotive": array:1 [
        0 => {#960
          +"OccurrenceCode": "06"
          +"OccurrenceMessage": "PAYED"
          +"Motives": array:1 [
            0 => null
          ]
        }
      ]
      +"CustomerName": "Customer Name"
      +"CustomerTaxNumber": "Tax Number"
      +"PayerBankCode": null
      +"PayerBankBranchCode": null
    }
    1 => {#961
      +"IdBoleto": 17614
      +"DueDate": "2019-01-28T00:00:00"
      +"PaymentDate": "2019-01-25T13:34:31.307"
      +"CreditDate": null
      +"PrincipalValue": 4.3
      +"InterestValue": 0.0
      +"FineValue": 0.0
      +"TotalValue": 4.3
      +"PaidValue": 4.3
      +"Status": 5
      +"MailToSend": "[email protected]"
      +"PhoneToSend": null
      +"Comments": null
      +"OurNumber": "000000002851"
      +"Identifier": "Teste06"
      +"ReturnMessage": null
      +"ReturnCode": null
      +"Barcode": null
      +"DigitableLine": null
      +"OccurrenceMotive": array:1 [
        0 => {#962
          +"OccurrenceCode": "06"
          +"OccurrenceMessage": "PAYED"
          +"Motives": array:1 [
            0 => null
          ]
        }
      ]
      +"CustomerName": "Customer Name"
      +"CustomerTaxNumber": "Tax Number"
      +"PayerBankCode": null
      +"PayerBankBranchCode": null
    }
  ]
}

I tried to manipulate and group the collection information, but it didn't work.

$payed->groupBy(function ($item){
    return Carbon::parse($item->DueDate)->format('Y-m');
});

I'm trying to group the data to have the information formatted as:

[
    {
        "quantity": 2,
        "week": 36,
        "totalValue": 8.9
    },
    {
        "quantity": 2,
        "week": 37,
        "totalValue": 10
    }
]

Could someone help me?

0 likes
6 replies
mstrauss's avatar

@conixs

What were the results when you tried the above groupBy collection method? And don't forget that the groupBy collection method, like most other collection methods, does not affect the original collection, so you have to assign it to a new var, like below:

$groupedCollection = $payed->groupBy(function ($item){
    return Carbon::parse($item->DueDate)->format('Y-m');
});
Conixs's avatar

It returns the following:

Collection {#977
  #items: array:1 [
    "2019-01" => Collection {#974
      #items: array:2 [
        0 => {#959
          +"IdBoleto": 17406
          +"DueDate": "2019-01-11T00:00:00"
          +"PaymentDate": "2019-01-11T18:44:57.937"
          +"CreditDate": null
          +"PrincipalValue": 4.5
          +"InterestValue": 0.0
          +"FineValue": 0.0
          +"TotalValue": 4.5
          +"PaidValue": 4.5
          +"Status": 5
          +"MailToSend": "[email protected]"
          +"PhoneToSend": null
          +"Comments": "Webhook"
          +"OurNumber": "000000002654"
          +"Identifier": "5"
          +"ReturnMessage": null
          +"ReturnCode": null
          +"Barcode": null
          +"DigitableLine": null
          +"OccurrenceMotive": array:1 [
            0 => {#960
              +"OccurrenceCode": "06"
              +"OccurrenceMessage": "PAYED"
              +"Motives": array:1 [
                0 => null
              ]
            }
          ]
          +"CustomerName": "Customer Name"
          +"CustomerTaxNumber": "Tax Number"
          +"PayerBankCode": null
          +"PayerBankBranchCode": null
        }
        1 => {#961
          +"IdBoleto": 17614
          +"DueDate": "2019-01-28T00:00:00"
          +"PaymentDate": "2019-01-25T13:34:31.307"
          +"CreditDate": null
          +"PrincipalValue": 4.3
          +"InterestValue": 0.0
          +"FineValue": 0.0
          +"TotalValue": 4.3
          +"PaidValue": 4.3
          +"Status": 5
          +"MailToSend": "[email protected]"
          +"PhoneToSend": null
          +"Comments": null
          +"OurNumber": "000000002851"
          +"Identifier": "Teste06"
          +"ReturnMessage": null
          +"ReturnCode": null
          +"Barcode": null
          +"DigitableLine": null
          +"OccurrenceMotive": array:1 [
            0 => {#962
              +"OccurrenceCode": "06"
              +"OccurrenceMessage": "PAYED"
              +"Motives": array:1 [
                0 => null
              ]
            }
          ]
          +"CustomerName": "Customer Name"
          +"CustomerTaxNumber": "Tax Number"
          +"PayerBankCode": null
          +"PayerBankBranchCode": null
        }
      ]
    }
  ]
}
mstrauss's avatar

How about something like the below? Just a heads up, this will not take into account the Year of the DueDate but per your requested format above, that doesn't seem to matter.



$groupedCollection = $payed->only('quantity', 'DueDate', 'TotalValue')->groupBy(function ($item){
    return Carbon::parse($item->DueDate)->week();
});
Conixs's avatar

It returns nothing

Collection {#973
  #items: []
}
mstrauss's avatar

Oh, sorry, perhaps it's weekOfYear and not just week. Also no need for the method parentheses (). Check out the docs: https://carbon.nesbot.com/docs/#api-getters


$groupedCollection = $payed->only('quantity', 'DueDate', 'TotalValue')->groupBy(function ($item){
    return Carbon::parse($item->DueDate)->weekOfYear;
});
Conixs's avatar

It still returns nothing, I think it is due to the only

Please or to participate in this conversation.