在 FullCalendar 4 中显示来自我的数据库的事件

Display events from my DB in FullCalendar 4

我正在尝试从 MySQL 数据库中的 Laravel 5.2 应用程序的全日历 4 页面上导入我的数据。

我的文件 Calendar.js :

import { Calendar } from '@fullcalendar/core';
import interactionPlugin from '@fullcalendar/interaction';
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGridPlugin from '@fullcalendar/timegrid';
import frLocale from '@fullcalendar/core/locales/fr';
import bootstrapPlugin from '@fullcalendar/bootstrap';

document.addEventListener('DOMContentLoaded', function () {
    var calendarEl = document.getElementById('calendar');

    var calendar = new Calendar(calendarEl, {
        plugins: [interactionPlugin, dayGridPlugin, timeGridPlugin, bootstrapPlugin],
        // height: 'parent',
        aspectRatio: 1.5,
        themeSystem: 'bootstrap',
        locale: frLocale,
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek,timeGridDay'
        },
        navLinks: true, // can click day/week names to navigate views
        editable: true,
        eventLimit: true,// allow "more" link when too many events
        events: 'getEvents',
    });
    calendar.render();
});

这里是Controller页面EventsController中的函数:

public function index(Request $request)
    {
        return view('front.events');
    }

public function getEvents()
    {
        $getEvents = Events::select('coach_id', 'start_date', 'end_date')->get();
        $events = [];

        foreach ($getEvents as $values) {
            $start_date_format = \DateTime::createFromFormat('Y-m-d h:i:s', $values->start_date);
            $end_date_format = \DateTime::createFromFormat('Y-m-d h:i:s', $values->end_date);
            $events ['title'] = $values->coach_id;
            $events ['start'] = $start_date_format->format('c');
            $events ['end'] = $end_date_format->format('c');
        }

        return $events;
    }

日历页面的路线部分:

Route::get('/events', 'EventsController@index')->name('events.index');
Route::get('/getEvents', 'EventsController@getEvents')->name('events.getEvents');

此处的视图 events.twig:

{% extends 'front.layouts.main' %} {% set user = auth_user() %}
{% block css %}
    <link href='https://use.fontawesome.com/releases/v5.0.6/css/all.css' rel='stylesheet'>
{% endblock %}
{% set user = auth_user() %} {% block content %}

    <div class="container mb-5">
        <div class="response"></div>
        <div id='calendar'></div>
    </div>

{% endblock %}

{% block js %}
<script src="{{ asset('js/calendar.js') }}"></script>
{% endblock %}

我可以看到事件 JSON 格式正确(当我加载我的页面 getEvents 时)但我的日历页面中没有任何内容。

我的JSON就是这样的:

{"title":4,"start":"2019-10-23T11:00:00+00:00","end":"2019-10-23T12:00:00+00:00"} 

我用 JSON lint 验证了它。

即使你只有一个事件,它也必须在一个数组中,fullCalendar 才能识别它,例如

[{"title":4,"start":"2019-10-23T11:00:00+00:00","end":"2019-10-23T12:00:00+00:00"}]

但我认为您的 PHP 的逻辑是错误的 - 它看起来会循环遍历您的所有事件,但会继续用下一个事件的值覆盖 $events 变量的属性在循环。因此,即使您的数据库中有 100 个事件,它也始终只输出一个,并且在序列化为 JSON 时,它始终是单个对象(而不是数组)。我认为您需要这样更改它:

public function getEvents()
{
    $getEvents = Events::select('coach_id', 'start_date', 'end_date')->get();
    $events = [];

    foreach ($getEvents as $values) {
        $start_date_format = \DateTime::createFromFormat('Y-m-d h:i:s', $values->start_date);
        $end_date_format = \DateTime::createFromFormat('Y-m-d h:i:s', $values->end_date);
        $event = [];
        $event['title'] = $values->coach_id;
        $event['start'] = $start_date_format->format('c');
        $event['end'] = $end_date_format->format('c');
        $events[] = $event;
    }

    return $events;
}

这会在 $event 中为数据库中的每条记录创建一个事件,然后在创建后将该新事件记录添加到整个 $events 列表中。