我從一個數據庫中獲取了此集合,其中有一個名爲「event_date」的列。laravel carbon從集合中的日期列中獲取日期名稱
我想要的僅僅是獲取集合中來自該列的日期名稱。
我知道你可以使用Carbon來獲取名稱天,例如,一種名爲->format()
的方法,但是我得到一個錯誤,說這種方法不存在。
到目前爲止我的代碼如下:
$collection = MyModel::all();
裏面有「活動日期」的屬性或列。從那以後,我想要得到日期的名字,將它們放入數組或集合中,最後計算那些日子的頻率。
爲了實現這一點,我已經試過如下:
我試過->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');
});
但我得到了以下錯誤:
Undefined property: Illuminate\Support\Collection::$myDates
任何想法只得到DayNames中到一個數組或集合?有沒有另一種方法來做到這一點?
不錯!但爲什麼我要將'$ Daynames'清空?如果我回顯每個'$ date->格式('l')'我可以看到日期名稱。但數組顯示爲空。我已經嘗試過使用($ Daynames)和'返回$ Daynames',它也不起作用。 – Pathros