0

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.

5
  • 1
    Why are you doing this by making the request yourself, then iterating over the data and adding individual events - and not via fullcalendar.io/docs/events-json-feed ?
    – CBroe
    Commented Jun 12 at 12:43
  • Have you checked what response this request actually got yet? If not, open your browser dev tools, and inspect the request in the network panel.
    – CBroe
    Commented Jun 12 at 12:45
  • @CBroe after I read your comment, and I looked again at my json data. The data returned by the quotations/events parameter is 2020. Meanwhile my data in the database is 2024, and when I try to look at the calendar for 2020 the data is displayed. now the problem is different. Why does my data which should have 2024 instead have 2020? Can you help me, where do I go wrong? Commented Jun 12 at 12:56
  • Probably because you parsed it wrong, with that 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 need Y for a four-digit year?
    – CBroe
    Commented Jun 12 at 13:03
  • Please take the tour. Begging is inappropriate. We all need help.
    – isherwood
    Commented Jun 12 at 14:05

1 Answer 1

0

Thanks to CBroe. Because of your comment I realized where the mistake was and I changed my controller to be like this.

 {
    $quotations = Quotation::where('status', 'deal')->get()->map(function ($quotation) {
        $cleanedDate = Carbon::createFromFormat('d-m-Y', $quotation->project_date)->format('Y-m-d');

        return [
            'title' => $quotation->project_name,
            'start' => $cleanedDate,
        ];
    });

    return response()->json($quotations);
}

and everything has been fine.

1
  • Some explanation of what you changed (so we don't have to compare line-by-line to figure it out) would be a great addition to this answer. Please see How to Answer.
    – isherwood
    Commented Jun 12 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.