【发布时间】:2017-07-06 11:08:43
【问题描述】:
我从一个数据库中收集了这个集合,其中有一个名为“event_date”的列。
我想要的只是从集合中的该列中获取日期名称。
我知道您可以使用 Carbon 来获取名称 days,例如,一个名为 ->format() 的方法,但是我收到一条错误消息,指出此方法不存在。
到目前为止我的代码如下:
$collection = MyModel::all();
里面有“event_date”属性或列。从中,我想获取日期的名称以将它们放入数组或集合中,最后计算这些日期的频率。
为了实现这一点,我尝试了以下方法:
我尝试了->pluck()方法如下:
$filtered = collect([
'myDates'=>$collection->pluck('event_date'),
]);
dd($filtered) 如下所示:
Collection {#209 ▼
#items: array:1 [▼
"myDates" => Collection {#243 ▼
#items: array:30 [▼
0 => Carbon {#244 ▼
+"date": "2017-02-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
1 => Carbon {#218 ▼
+"date": "2017-01-15 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
2 => Carbon {#250 ▼
+"date": "2016-09-25 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
3 => Carbon {#249 ▼
+"date": "2016-05-22 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
...
我尝试按如下方式获取日期名称:
$Daynames = $filtered->each(function($item,$key){
return $item->myDates->format('l');
});
但我收到以下错误:
未定义属性:Illuminate\Support\Collection::$myDates
有什么想法可以只将日期名放入数组或集合中吗?还有其他方法吗?
【问题讨论】:
标签: php laravel laravel-5 php-carbon