$collection=Event::all();
$keyed = $collection->mapWithKeys(function ($item) {
return ['title' => $item['name'], 'start' => $item['event_date']];
});
$keyed->all()
被收集在改变键只返回最后一个项目:
Array
(
[title] => New Year
[start] => 2018-01-01
)
我如何获得与改变按键的所有活动?
怎么办如果将'dd($ collection-> toArray());''放在'$ collection = Event :: all();'后面? – whoacowboy
@whoacowboy'dd($ collection-> toArray());'given给出'array:3 [▼ 0 => array:5 [▼ “id”=> 1 “name”=>“Parent's Day” “event_date”=>“2018-02-14” ] 1 => array:5 [▼ “id”=> 2 “name”=>“Labor's Day \ n” “event_date”=>“2018 -05-01" ] 2 =>数组:5 [▼ “ID”=> 3 “名称”=> “新年” “EVENT_DATE”=> “2018年1月1日” ] ] '而'dd($ keyed-> toArray());'只给出最后一个事件。 – Steve