I want to display dates based on project dates from Quotation data that have deal status in my database. and I use fullcalendar as the UI. however my script fails to display the json data sent by my controller. the ui view of my fullcalendar only displays today's date.
This is the script that I made for the blade view page.
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth'
});
$.ajax({
url: "{{ route('calendar.quotations') }}",
type: 'GET',
dataType: "json",
success: function(data) {
data.forEach(function(event) {
calendar.addEvent({
title: event.title,
start: event.start
});
});
},
});
calendar.render();
});
</script>
this is my route
Route::get('calendar', [App\Http\Controllers\CalendarController::class, 'index'])->name('calendar.index')->middleware('auth');
Route::get('quotations/events', [App\Http\Controllers\CalendarController::class, 'getQuotations'])->name('calendar.quotations')->middleware('auth');
and the controller is like this.
public function index()
{
return view('calendar.index');
}
public function getQuotations()
{
$quotations = Quotation::where('status', 'deal')->get()->map(function ($quotation) {
$cleanedDate = substr($quotation->project_date, 0, 8);
$startDate = Carbon::createFromFormat('d-m-y', $cleanedDate)->format('Y-m-d');
return [
'title' => $quotation->project_name,
'start' => $startDate,
];
});
return response()->json($quotations);
}
I need help. I want to display dates based on project dates from Quotation data that have deal status in my database. and I use fullcalendar as the UI.
Carbon::createFromFormat('d-m-y', $cleanedDate)
line there. What does $cleanedDate actually contain?y
is a two-digit year, I'm guessing you'd actually needY
for a four-digit year?