我想循环每一个约会并把它放在json中:
[
{
"id":"1",
"title":"Test 1",
"start":"2016-09-16 12:00:00",
"end":"2016-09-16 14:00:00",
"allDay":false
},
{
"id":"2",
"title":"Test 2",
"start":"2016-09-15 12:00:00",
"end":"2016-09-15 14:00:00",
"allDay":false
}
]
我在数据库中有一个表appointments
。
id | title | appointment_date_start | appointment_date_end |
-------------------------------------------------------------
1 | Test 1 | 2016-09-16 12:00:00 | 2016-09-16 14:00:00 |
2 | Test 2 | 2016-09-15 12:00:00 | 2016-09-15 14:00:00 |
在我的feed.blade.php
中,我获得了所有数据。
所以我能做到:
@foreach ($appointments as $appointments)
{{ $appointment->id }}
{{ $appointment->title }}
{{ $appointment->appointment_date_start }}
{{ $appointment->appointment_date_end }}
@endforeach
这将输出数据库中的数据。但是我如何在数组中预先处理它呢?
像
$event_array[] = array(
'id' => {{ $appointment->id }},
'title' => {{ $appointment->gender }},
'start' => {{ $appointment->appointment_date_start }},
'end' => {{ $appointment->appointment_date_end }},
'allDay' => false
);
修改
我在公共职能部门尝试了这个
public function feed()
{
$feed = Appointment::all();
return $feed->toJson();
return view('appointments/feed');
}
这就是我得到的
{
id: 35,
user_id: 1,
gender: "men",
appointment_date_start: "2016-09-14 20:00:00",
appointment_date_end: "2016-09-14 20:40:00",
created_at: "2016-09-16 08:16:16",
updated_at: "2016-09-16 08:16:16"
}
我需要删除gender, created_at and updated_at
,并且应该启动appointment_date_start并且应该结束appointment_date_end吗?这可能吗?
答案 0 :(得分:1)
你可以去{{ $appointments->toJson() }}
,这就是你想要的吗?
答案 1 :(得分:1)
如果要删除created_at和updated_at,只需在从数据库中提取数据时选择属性:
$feed = Appointment::select('id', 'user_id', 'appointment_date_start as start', 'appointment_date_end as end')->get();
// leave created_at and updated_at
// select('old as new') to change the attribute name
然后使用json_encode
return json_encode($feed);