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

Daniel-Pablo's avatar

help me to collect the month of this array please!

look I got this so far

$infomation = '[{ "name": "Necklace", "created_at": "2019-03-01" }, { "name": "Bracelet", "created_at": "2019-03-05" }, { "name": "Dog Chain", "created_at": "2020-05-27" }]';

$array = json_decode($infomation, true);
$collection = collect($array);

output

Illuminate\Support\Collection {#2360
  all: [
    [
      "name" => "Necklace",
      "created_at" => "2019-03-01",
    ],
    [
      "name" => "Bracelet",
      "created_at" => "2019-03-05",
    ],
    [
      "name" => "Dog Chain",
      "created_at" => "2020-05-27",
    ],
  ],
}

I want just the create_at of march, in this case is 03 the month so I need to get in the collection just necklace and bracelet not dog chain how-to?

so I try to use this but got an error

$collection->whereMonth('created_at', '=', '03')
    ->get();

BadMethodCallException with message 'Method Illuminate\Support\Collection::whereMonth does not exist.'

0 likes
6 replies
Snapey's avatar

where does this data come from? Doing it in the collection is the least optimal method as the date is not a carbon instance, its just a string. Since it is created_at it sounds like it came from a model????

You want march records - any march - in any year?

$marchRecords = $collection->filter(function($item) {
    return Carbon::parse(item['created_at'])->format('m') == '03';
  });
Daniel-Pablo's avatar

@Snapey this code comes from a rought string :( I was like WHATTTT!! hahaha first time I saw that

Daniel-Pablo's avatar

@Snapey just march but got this error

$infomation = '[{ "name": "Necklace", "created_at": "2019-03-01" }, { "name": "Bracelet", "created_at": "2019-03-05" }, { "name": "Dog Chain", "created_at": "2020-05-27" }]';

$array = json_decode($infomation, true);
$collection = collect($array);


$marchRecords = $collection->filter(function($item) {
    return Carbon\Carbon::parse(item['created_at'])->format('m') == '03';
  });


$marchRecords;

ErrorException with message 'Undefined array key "file"'

you can try it on your tinkerwell?...

Snapey's avatar
Snapey
Best Answer
Level 122

@Arius Tigger small typo, missing $ before item

return Carbon\Carbon::parse($item['created_at'])->format('m') == '03';

I have sent a support email to Beyondcode about Tinkerwell returning this strange "file" error when inside a closure

1 like
Daniel-Pablo's avatar

@Snapey Yes it works! , I was wondering if you know why I can't use the WhereMonth method? Why I get this error Method Illuminate\Support\Collection::whereMonth does not exis and also how I could use it in this case... I ask because I want to learn more about it for a next challenge, thanks in advance and thanks for helping me with the previous code, I really appreciate it! @snapey

Snapey's avatar

@Arius Tigger whereMonth is a database query builder function. This is a Collection. There is no such method on a collection. Plus, your dates are just strings so cannot be compared to dates without first being converted.

Please or to participate in this conversation.