我正在尝试将事件加载到日历中。
因此我需要使用事件数据创建一个JSONstring,但我无法弄清楚如何执行此操作。
我如何创建数据:
var colors = ['#3B97B2', '#67BC42', '#FF56DE', '#E6D605', '#BC36FE', '#000'];
$('#bar_chart').highcharts({
chart: {
type: 'column'
},
title: {
text: ''
},
subtitle: {
text: ''
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: ''
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: false
}
}
},
series: [{
name: '',
colorByPoint: true,
data: [{
name: 'Blue',
y: 5.78,
color: colors[0]
}, {
name: 'Green',
y: 5.19,
color: colors[1]
}, {
name: 'Pink',
y: 32.11,
color: colors[2]
}, {
name: 'Yellow',
y: 10.04,
color: colors[3]
}, {
name: 'Purple',
y: 19.33,
color: colors[4]
}]
}]
});
返回:
$tickets = Ticket::where('mechanic_id', $request->mechanic_id)->get();
$tickets_json[] = [];
foreach($tickets as $ticket) {
$tickets_json['title'] = $ticket->number;
$tickets_json['start'] = $ticket->planned_timestamp_start->format('d-m-Y H:i');
$tickets_json['end'] = $ticket->planned_timestamp_end->format('d-m-Y H:i');
$tickets_json['url'] = '/tickets/' . $ticket->id;
}
这不是我想要和需要的。
我需要这样:
array:5 [▼
0 => []
"title" => "452"
"start" => "27-10-2017 09:00"
"end" => "27-10-2017 12:00"
"url" => "/tickets/2"
]
我试过了:
{
title: '63782136',
start: '27-10-2017 09:00',
end: '"27-10-2017 12:00"',
url: '/tickets/2'
},
{
title: '23123',
start: '27-10-2017 09:00',
end: '"27-10-2017 12:00"',
url: '/tickets/3'
},
{
title: '432512',
start: '27-10-2017 09:00',
end: '"27-10-2017 12:00"',
url: '/tickets/4'
},
但是没有返回正确的格式:
return response()->json($tickets_json);
添加{“0”:[],}
提前致谢。
答案 0 :(得分:3)
如果您希望在第二维中将数据分配到数组的第一维,则会犯错误。以下应该可以解决问题:
$tickets_json = [];
$tickets->each(function ($ticket) use (&$tickets_json) {
$tickets_json[] = [
'title' => $ticket->numer,
'start' => $ticket->planned_timestamp_start->format('d-m-Y H:i'),
'end' => $ticket->planned_timestamp_end->format('d-m-Y H:i'),
'url' => '/tickets/' . $ticket->id
];
});
return $tickets_json;
此代码将为您提供正确的格式化数组,您可以使用它来获取以下JSON输出:
[
{
"title": "63782136",
"start": "27-10-2017 09:00",
"end": "27-10-2017 12:00",
"url": "/tickets/2"
},
{
"title": "23123",
"start": "27-10-2017 09:00",
"end": "27-10-2017 12:00",
"url": "/tickets/3"
}
]