How to receive FullCalendar events from laravel .. controller?

I am working on a complete calendar page for my site. Their documentation doesn't explain much of how to fetch events from a controller url. Now I can add new events to the database .. but those events are not showing in the calendar .. and for the same reason I cannot update or remove events from it.

This is the javascript code on my calendar.php view page.

<script src='http://fullcalendar.io/js/fullcalendar-2.3.1/lib/moment.min.js'></script>
<script src='http://fullcalendar.io/js/fullcalendar-2.3.1/lib/jquery.min.js'></script>
<script src='http://fullcalendar.io/js/fullcalendar-2.3.1/lib/jquery-ui.custom.min.js'></script>
<script src='http://fullcalendar.io/js/fullcalendar-2.3.1/fullcalendar.js'></script>

$(document).ready(function() {

    var currentMousePos = {
        x: -1,
        y: -1
    };

    jQuery(document).on("mousemove", function (event) {
        currentMousePos.x = event.pageX;
        currentMousePos.y = event.pageY;
    });

    /* initialize the external events
     -----------------------------------------------------------------*/


    $('#external-events div.external-event').each(function() {

        // store data so the calendar knows to render an event upon drop
        $(this).data('event', {
            title: $.trim($(this).text()), // use the element text as the event title
            stick: true // maintain when user navigates (see docs on the renderEvent method)
        });

        // make the event draggable using jQuery UI
        $(this).draggable({
            zIndex: 1111999,
            revert: true,      // will cause the event to go back to its
            revertDuration: 0  //  original position after the drag
        });

    });


    /* initialize the calendar
     -----------------------------------------------------------------*/
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    var calendar=$('#calendar');

    calendar.fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar
        drop: function() {
            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
                // if so, remove the element from the "Draggable Events" list
                $(this).remove();
            }
        },

        eventSources: [

            {

                url: '/v1/calendar/',
                type: 'GET',
                dataType:'json',


            },

        ],

        selectable: true,
        selectHelper: true,
        select: function(start, end, allDay) {

            bootbox.prompt("New Event Title:", function(title) {
                var people_id=1;
                //var title=event.title;
                //var start=event.start;
                //var end=event.end;

                if (title !== null) {
                    calendar.fullCalendar('renderEvent',
                            {
                                people_id:people_id,
                                title: title,
                                start: start,
                                end: end,
                                allDay: allDay
                            },

                    true // make the event "stick"


                            );

                            var myDate = new Date( start *1000);
                            var startdate=myDate.toLocaleString();
                            var Datetwo = new Date( end *1000);
                            var enddate=Datetwo.toLocaleString();




                            $.ajax({
                                 url: '/v1/calendar',
                                 data: 'people_id='+people_id+'&title='+title+'&start='+startdate+'&end='+enddate,
                                /*data: {  
                                    people_id:'people_id',
                                         title: 'title',
                                    start:'startdate',
                                    end:'enddate'
                                     },*/
                                 type: 'POST',
                                 dataType: 'json',
                                 success: function(response){
                                     bootbox.alert("Event Created!");
                                   //$('#calendar').fullCalendar('updateEvent',event);
                                   console.log(response);
                                 },
                                 error: function(e){
                                   console.log(e.responseText);
                                 }
                               });  

                }
            });



            //calendar.fullCalendar('unselect');

        }
        ,
        eventClick: function(calEvent, jsEvent, view) {

            var form = $("<form class='form-inline'><label>Change event name &nbsp;</label></form>");
            form.append("<input class='middle' autocomplete=off type=text value='" + calEvent.title + "' /> ");
            form.append("<button type='submit' class='btn btn-sm btn-success'><i class='icon-ok'></i> Save</button>");

            var div = bootbox.dialog({
                message: form,
                buttons: {
                    "delete": {
                        "label": "<i class='icon-trash'></i> Delete Event",
                        "className": "btn-sm btn-danger",
                        "callback": function() {
                            calendar.fullCalendar('removeEvents', function(ev) {
                                //alert("removed");
                                //return (ev._id == calEvent._id);
                                $.ajax({
                                    url: '/v1/calendar/'+event.event_id+'/destroy',
                                    data: 'type=remove&eventid='+event.id,
                                    type: 'POST',
                                    dataType: 'json',
                                    success: function(response){
                                        console.log(response);
                                        if(response.status == 'success')
                                            jQuery('#calendar').fullCalendar('removeEvents', event.id);
                                    },
                                    error: function(e){ 
                                        alert('Error processing your request: '+e.responseText);
                                    }
                                });
                            })
                        }
                    },
                    "close": {
                        "label": "<i class='icon-remove'></i> Close",
                        "className": "btn-sm"
                    }
                }

            });

            form.on('submit', function(event) {
                calEvent.title = form.find("input[type=text]").val();
                calendar.fullCalendar('updateEvent', calEvent);
                div.modal("hide");

                //return false;
                alert(event.event_id);
              $.ajax({
                    url: '/v1/calendar/'+event.event_id+'/update',
                    //data: 'title='+title+'&eventid='+event.event_id,
                    data: {
                             title: 'title',
                             event_id: 'event_id'
                         },
                    type: 'POST',
                    dataType: 'json',
                    success: function(response){    
                        if(response.status == 'success')                            
                            $('#calendar').fullCalendar('updateEvent',event);
                            //alert("updated");
                            console.log(response);
                    },
                    error: function(e){
                        alert('Error processing your request: '+e.responseText);
                    }

                });



            });

        }


    });


});

      

and this is my CalendarController.php file

    <?php

class CalendarController extends \BaseController {

/**
 * Display a listing of calendar
 *
 * @return Response
 */
public function index()
{
    $event = DB::table('events')

    ->leftJoin('people','people.people_id','=','events.people_id')  
    ->leftJoin('people_roles','people_roles.people_id','=','events.people_id')      
    ->get(array('people.address_id','people.people_id','people.occupation','people.firstname','people.lastname','people.comment','people.gender','people.middlename','people_roles.school_year','people_roles.teacher','people_roles.parent','people_roles.teacher_a_id','people_roles.admin','events.event_id','events.evt_description','events.date1','events.date2','events.time')); 
    //return View::make('people.show', compact('address'));
    return Response::json($event);
}

/**
 * Show the form for creating a new calendar
 *
 * @return Response
 */
public function create()
{
    return View::make('calendar.create');
}

/**
 * Store a newly created calendar in storage.
 *
 * @return Response
 */
public function store()
{
    $events= Input::get('type');
    $events= new Events;
    $events->people_id = Input::get('people_id');
    $events->evt_description =Input::get('title');
    $events->date1 =Input::get('start');
    $events->date2 =Input::get('end');
    //$events->time =Input::get('time');

    $events->save();
    //$validator = Validator::make($data = Input::all(), Events::$rules);

    /*if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }*/
    //Calendar::create($data);
    return Response::json($events);
    //return Redirect::route('calendar.index');
}

/**
 * Display the specified calendar.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $calendar = Calendar::findOrFail($id);

    return View::make('calendar.show', compact('calendar'));
}

/**
 * Show the form for editing the specified calendar.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $calendar = Calendar::find($id);

    return View::make('calendar.edit', compact('calendar'));
}

/**
 * Update the specified calendar in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    //$type=Input::get('type');
    $event_id= Input::get('event_id');
    $title= Input::get('title');
    $roles = DB::table('events')
                ->where('event_id','=',$event_id )
                ->update(array('evt_description' => $title));

     return Response::json(array('eventid'=>$event_id,'title'=>$title));

    /*$calendar = Calendar::findOrFail($id);

    $validator = Validator::make($data = Input::all(), Calendar::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $calendar->update($data);

    return Redirect::route('calendar.index');*/

}

/**
 * Remove the specified calendar from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy()
{
//  Calendar::destroy($id);
$event_id= Input::get('eventid');
DB::table('events')->where('event_id','=',$event_id)->delete();

return Response::json($event_id);

//  return Redirect::route('calendar.index');
}

}

      

Please help me here ...

+3


source to share





All Articles