我在laravel框架上遇到Fullcalendar的问题。
我的代码如下:
加载fullcalendar时调用事件的脚本:
eventSources:
[
{
type:'POST',
url: '/hotel/public/api/checkFullCalendar',
dataType: 'json',
data: {
"_token": "{{ csrf_token() }}"
}
}
]
这是我的控制器:
public function checkFullCalendar(Request $request) {
$today = Carbon::now()->format('Y-m-d');
$reservation = Reservation::where('checkin','>=', Carbon::now()->startOfMonth())->get();
$events = array();
foreach ($reservation as $reser) {
$e = array();
$e['id'] = $reser->id;
$e['title'] = "Test";
$e['start'] = $reser->checkin;
$e['end'] = $reser->checkout;
array_push($events, $e);
}
return response()->json(['events' , $events]);
}
这是输出:
["events",[{"id":1,"title":"Test","start":"2017-09-01","end":"2017-09-02"},{"id":2,"title":"Test","start":"2017-09-01","end":"2017-09-02"},{"id":3,"title":"Test","start":"2017-09-01","end":"2017-09-03"}]]
并且它不会抛出任何错误,日历上也不显示任何事件,但是,但是, 如果我只更改我的查询只有1条记录,这就是工作
public function checkFullCalendar(Request $request) {
$today = Carbon::now()->format('Y-m-d');
$reservation = Reservation::where('checkin','>=', Carbon::now()->startOfMonth())->first();
$events = array();
$events['id'] = $reservation->id;
$events['title'] = "Test";
$events['start'] = $reservation->checkin;
$events['end'] = $reservation->checkout;
return response()->json(['events' , $events]);
}
它工作得很好(下面输出)如果我只有1条记录我想我必须设置数组输出的样式错误任何想法我在这里缺少什么?谢谢
["events",{"id":1,"title":"Test","start":"2017-09-01"}]
答案 0 :(得分:0)
您的输出必须是这样的单个数组:
[
{"id":1,"title":"Test","start":"2017-09-01","end":"2017-09-02"}
{"id":2,"title":"Test","start":"2017-09-01","end":"2017-09-02"}
{"id":3,"title":"Test","start":"2017-09-01","end":"2017-09-03"}
]
没有外围阵列。 fullCalendar正在外部数组的根目录中查找事件对象。它认为整个响应是事件数组,它不期望必须查看外部数组的任意元素。
我不认识Laravel,但我认为写作
return response()->json($events);
控制器中的会产生正确的结果。
P.S。它与一个事件一起运行的原因只是运气 - 你有一个单独的数组,它将遍历它们并可能忽略第一个元素(字符串“events”),因为它不是一个有效的事件对象然后管理到显示第二个元素,因为它是有效的。而当第二个元素也是一个数组时,它不知道该怎么做 - 它直接期望事件对象,而不是子数组。