简体   繁体   中英

Need help figuring out why Fullcalendar isn't showing my events. - no errors showing

I am using Laravel 7. I followed the instructions from the Fullcalendar website and have searched for days trying to figure this out.

For some reason, I can't pull or save data. I've tried doing a die-dump and it's like the code isn't firing. Maybe someone can see something I'm missing. I am using the cdn's for ajax, jquery and fullcalendar.

Here is the code for the controller:

public function index()
{
    if(request()->ajax())
    {

        $start = (!empty($_GET["start"])) ? ($_GET["start"]) : ('');
        $end = (!empty($_GET["end"])) ? ($_GET["end"]) : ('');

        $data = Event::whereDate('start', '>=', $start)->whereDate('end',   '<=', $end)->get(['id','title','start', 'end']);
        return Response::json($data);
    }
    return view('calender');
}


public function create(Request $request)
{
    $insertArr = [ 'title' => $request->title,
        'start' => $request->start,
        'end' => $request->end
    ];
    $event = Event::insert($insertArr);
    return Response::json($event);
}


public function update(Request $request)
{
    $where = array('id' => $request->id);
    $updateArr = ['title' => $request->title,'start' => $request->start, 'end' => $request->end];
    $event  = Event::where($where)->update($updateArr);

    return Response::json($event);
}


public function destroy(Request $request)
{
    $event = Event::where('id',$request->id)->delete();

    return Response::json($event);
}

Here is the script from the view:

$(document).ready(function () {

    var SITEURL = "{{url('/')}}";
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    var calendar = $('#calendar').fullCalendar({
        editable: true,
        events: SITEURL + "calendar",
        displayEventTime: true,
        editable: true,
        eventRender: function (event, element, view) {
            if (event.allDay === 'true') {
                event.allDay = true;
            } else {
                event.allDay = false;
            }
        },
        selectable: true,
        selectHelper: true,
        select: function (start, end, allDay) {
            var title = prompt('Event Title:');

            if (title) {
                var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss");
                var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");

                $.ajax({
                    url: SITEURL + "calendar/create",
                    data: 'title=' + title + '&amp;start=' + start + '&amp;end=' + end,
                    type: "POST",
                    success: function (data) {
                        displayMessage("Added Successfully");
                    }
                });
                calendar.fullCalendar('renderEvent',
                    {
                        title: title,
                        start: start,
                        end: end,
                        allDay: allDay
                    },
                    true
                );
            }
            calendar.fullCalendar('unselect');
        },

        eventDrop: function (event, delta) {
            var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
            var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
            $.ajax({
                url: SITEURL + 'calendar/update',
                data: 'title=' + event.title + '&amp;start=' + start + '&amp;end=' + end + '&amp;id=' + event.id,
                type: "POST",
                success: function (response) {
                    displayMessage("Updated Successfully");
                }
            });
        },
        eventClick: function (event) {
            var deleteMsg = confirm("Do you really want to delete?");
            if (deleteMsg) {
                $.ajax({
                    type: "POST",
                    url: SITEURL + 'calendar/delete',
                    data: "&amp;id=" + event.id,
                    success: function (response) {
                        if(parseInt(response) > 0) {
                            $('#calendar').fullCalendar('removeEvents', event.id);
                            displayMessage("Deleted Successfully");
                        }
                    }
                });
            }
        }

    });
});

function displayMessage(message) {
    $(".response").html("<div class='success'>"+message+"</div>");
    setInterval(function() { $(".success").fadeOut(); }, 1000);
}

Finally figured this out. Thanks to everyone that responded, Once I started a new Laravel project. I was able to pinpoint what I was missing.

It turns out that there were a couple issues:

The first one was that the docs on the Fullcalendar.io website weren't very helpful. It gives you a place to start, but then you are on your own to figure out what's missing in the code. My code needed this added to the script in the view:

var calendar = new Calendar(calendarEl, {

events: '/myurlfeed.php' });

The second is that the tutorial I followed included an if statement saying that if it was ajax, then execute code. Once I took out the if portion, it directly accessed the code and was able to load from the database. Whew. Lots of hours spent trying to figure this out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM