Is the JSON response completely empty or does it have some invalid characters? Looking at that output will help determine the next step.
Laravel 8 + FullCalendar 5 - Getting the events issue
I tried and went through dozens of blogposts and even some threads here on Laracasts, but no luck. I'm implementing a FullCalendar (with v5, I noticed it does matter since most blogposts I stumbled upon had some code that was depreciated) with a Laravel 8 project.
The issue I'm facing is that I can't get events to show up - I do get them in a JSON format out of the database, but I have trouble invoking that controller function - I'm basically sure it's just a semantic issue, but here it is.
So, this is my EventsController:
class EventsController extends Controller
{
public function index(Request $request)
{
$getEvents = Event::select('event_date', 'event_content')->get();
$events = [];
foreach ($getEvents as $values) {
$event = [];
$event['title'] = $values->event_content;
$event['start'] = $values->event_date;
$event['allDay'] = true;
$event['editable'] = false;
$events[] = $event;
}
return response()->json($events);
}
If I just return $events or return response()->json($events);-> I get this JSON (and that's ok, so, data is out of the DB):
[{"title":"John Doe - Hospital","start":"2022-08-18","allDay":true,"editable":false},{"title":"Mary Joe - Factory","start":"2022-08-19","allDay":true,"editable":false}]
This is my route:
Route::get('getEvents', 'App\Http\Controllers\EventsController@index')->name('getEvents');
and this is the script that initiates the calendar, placed inside the calendar.blade.php
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
editable: false,
droppable: true,
selectable: true,
initialView: 'dayGridWeek',
views: {
dayGridWeek: {
type: 'dayGridWeek',
duration: { weeks: 2 },
buttonText: '4 day'
}
},
themeSystem: 'bootstrap5',
editable: false,
events: 'getEvents',
displayEventTime: false,
But every time I get the error that Failure parsing JSON
Object { message: "Failure parsing JSON", xhr: XMLHttpRequest }
main.js:6262:21
and when I look at the source code, there really aren't any events there. I think that the problem is somewhere in this events: 'getEvents' part - I would expect that 'getEvents' would somehow invoke the index function and get all the events, and that would work. If I hardcoded the JSON response into "events", everything works fine.
So, if anyone has any ideas on what to try out next - I would really appreciate it.
Please or to participate in this conversation.