Mat
Mat

Reputation: 504

Fullcalendar JSON Encode and Laravel

I've got a calendar showing on a page but I can't for the life of me get the thing working. I'm making an array within my controller, passing it to the view json_encoded but the fullcalendar doesn't want to pick the array up and pop it into the calendar. Please see my code below:

HomeController

    public function calendar_feed(){

  return response()->json(
  [
    'title' => 'Matts Booking',
    'start' => '2018-01-01T22:40',
    'end' => '2019-01-01T23:40'
  ], 200
  );

}

Fullcalendar Function

<script type="text/javascript">
    (function ( $ ) { 

  $('#calendar').fullCalendar({
    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay,listWeek'
    },
    editable: false,
    defaultView: 'agendaWeek',
    firstDay: 1,
    eventLimit: false, // allow "more" link when too many events
    navLinks: true,
    events: '{{ route("calendar_feed") }}'
  });
    }( jQuery ));
</script>

What the array is sending

{"title":"Matts Booking","start":"2018-01-01T22:40","end":"2019-01-01T23:40"}

Any help would be appreciated!

Many thanks

Upvotes: 0

Views: 493

Answers (1)

shukshin.ivan
shukshin.ivan

Reputation: 11340

According to the name of the property events and array example, looks like it needs an array of it, so add an extra pair of square brackets:

public function calendar_feed(){
  return response()->json([[
      'title' => 'Matts Booking',
      'start' => '2018-01-01T22:40',
      'end' => '2019-01-01T23:40'
   ]], 200
  );
}

Now you return an array of objects with single element.

Upvotes: 1

Related Questions