Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

blackshtef's avatar

Deleting a FullCalendar event - need help on getting the ID

So, I have a FullCalendar implemented in my Laravel app like so:

Blade template:

<!-- Event data that shows up in the event detail modal -->
eventClick: function(calendar, jsEvent, view) {
                    $('#eventDetail').modal("show");
                    $('#id').html(calendar.event.id);
                    $('#title').html(calendar.event.title);
                    $('#start').html(moment(calendar.event.start).format('DD.MM.YYYY'));
                    $('#volunteer').html(calendar.event.extendedProps.volunteer);
                }

<!-- The modal, that shows event details and has a delete button -->
<div class="modal-body">
                    Event ID: <p id="id"></p>
                    Start: <p id="start"></p>
                    Description: <p id="volunteer"></p>
                    Title: <p id="title"></p>
  </div>
                <div class="modal-footer">
                    <form method="POST" action="{{ route('calendar.destroy') }}">
                        <div class="form-group">
                            <x-button tabindex="3">
                                {{ __('Delete Event') }}
                            </x-button>
                        </div>
                    </form>

Controller:

//getting the data from the DB and preparing it for the calendar script
->map(fn ($events) => [
            'id' => $events->event_id,
            'title' => $events->event_content,
            'start' => $events->event_date,
            'allDay' => true,
            'editable' => false,
            'backgroundColor' => $events->hospital_color,
            'borderColor' => $events->hospital_color,
            'hospital_max_people' => $events->hospital_max_people,
            'volunteer' => User::find($events->user_id)->first_name .' '. User::find($events->user_id)->last_name
        ]);

//destroy function
public function destroy(Request $request): \Illuminate\Http\JsonResponse
    {
        $data = [
            'event' => Event::destroy($request->id),
            'success' => true,
        ];

Route:

Route::delete('calendar/destroy',[\App\Http\Controllers\EventsController::class,'destroy'])->name('calendar.destroy');

The displaying of event details works fine - and now it is time to implement deleting of the events, and I got stuck with two things:

  1. How to secure this route to avoid being called from directly from a browser? I don't want to allow that people can just enter mysite.com/calendar/destroy/123 and delete stuff directly?
  2. More important in this stage - when I try to delete an event in a setup like this, I will get an error saying that
The GET method is not supported for this route. Supported methods: DELETE.

But I'm not using GET, or am I?

And final part: How to I get an ID of the event? I get it in the controller for all events and pass that to the FullCalendar script, but I'm not sure how to use that ID here. How to "add" it to the route / form action URL so that final link becomes calendar/destory/456?

0 likes
9 replies
vincent15000's avatar

Hello, here are some ideas.

  • you can secure your route with a middleware, so if anybody try to delete an event, he won't be able because the action will be forbidden by the middleware

  • to delete an event, instead of using a form submission, I would rather send an ajax request with the id of the event to delete

  • when you describe the action in the form submission route('calendar.destroy'), the id of the event is missing, the csrf token is missing and the delete method is missing

<form method="POST" action="{{ route('calendar.destroy', ['id' => $event->id]) }}">
	@csrf
	@method('delete')
	<div class="form-group">
		<x-button tabindex="3">
			{{ __('Delete Event') }}
		</x-button>
	</div>
</form>
blackshtef's avatar

@vincent15000 Thanks for the tips. I have added csrf token and delete method, and this is the actual issue - I have also added ['id' => $event->id] but I get Undefined variable $event error. I can get the id displayed in a html element (eg. <p id="id"></p>) but no idea how to put it in a variable.

Also, do I have to define my delete route as Route::delete(calendar.destroy), or Route::delete(calendar.destroy/{id})?

1 like
vincent15000's avatar

@blackshtef When you display the modal to delete an event, you necessarily send the event datas to the modal.

When I wrote ['id' => $event->id], I assumed you send the $event to the modal. Sure you have to replace with the event id.

In your case the best way would probably be to manage the destroy action via ajax rather than the form action method.

I also use fullcalendar but I don't manage the event deletion this way. When I open my modal to destroy an event, I fill the modal from the database and not from the calendar itself, so I can pass the needed datas to the modal, including the event id.

blackshtef's avatar

@vincent15000 Yes, I'm sending the data to the modal within the calendar script:

events: 'getEvents', //getEvents is the function within the Events controller that gets all the events
eventClick: function(calendar, jsEvent, view) {
                    $('#eventDetail').modal("show");
                    $('#id').html(calendar.event.id);
                    $('#title').html(calendar.event.title);
                    $('#start').html(moment(calendar.event.start).format('DD.MM.YYYY'));
                    $('#volunteer').html(calendar.event.extendedProps.volunteer);
                }

And then I display that data with eg. <p id="title"></p> etc. There were two reasons why I went this way:

  1. If I took all the events from the DB and just foreach ($events as $event) { show modal... } I would have all those events in the source code of the site - I thought that would be poor performance. I also ran into an issue that any event I clicked, it would show the details of the first event (and I didn't have the idea on how to fix that).
  2. If I didn't use the eventClick with these parameters, I'm not sure how else would I be able to send the event data to the modal.

So, my thinking was - get the data from the DB, FullCalendar will call that function, and that's about it. But I will try other options :)

You said that you fill modals from the database - would you be able to explain how? Because as I mentioned, I tried that with the foreach loop, but in that case, every modal would load the data of the first event. And I think that there might be some solution by using eventID, but I think I would run into the same issue as I have now - getting the eventID from the calendar.

1 like
vincent15000's avatar

@blackshtef I retrieve the event id at the moment I click on the event and I retrieve the datas from this event in the database.

eventClick: function(info) {
	edit(info.event.extendedProps.course_id)
},

Here my edit method will assign the right id and show the modal. When my modal loads, I retrieve the datas of this specific event (via its id) to display them in the modal. You can see that I have added an extended props (you can see how it works in the fullcalendar documentation).

I do it with VueJS, but you can do something similar in JavaScript inside a blade view.

blackshtef's avatar

Sorry, I haven't got the time in last couple of days to try this out - as soon as I do and get to the solution, I'll close the post :)

1 like
blackshtef's avatar

I managed to solve the issue and just wanted to post the solution here :) So basically, I used a hidden input field in the delete form. At first I had some issues on how to fill out the value parametar of that hidden input, but then I used .attr('value', calendar.event.id) selector to fill it out. I also had to add id parameter to my delete route.

I do have one additional question :) You'll notice that everywhere I'm using the term id, and not event_id. But my model file, as well as the actual column name in the DB is "event_id" (I know this isn't to Laravel naming standard). Regardless, the application is able to delete the events - how? Is it Laravel magic, or did I miss anything? The same result is even if I use Route::DELETE('calendar/destroy/{id}' or Route::DELETE('calendar/destroy/{event_id}' How is that possible?

Anyway, this is what my code looks like now:

Model:

class Event extends Model
{
    use HasFactory;
    protected $fillable = [
        'event_id',
        'user_id',
        'hospital_id',
        'report_id',
        'event_date',
        'event_content',
        'created_by'
    ];
    protected $primaryKey = 'event_id';
    protected $table = 'events';
}

Route (web.php):

Route::DELETE('calendar/destroy/{id}',[\App\Http\Controllers\EventsController::class,'destroy'])->name('calendar.destroy');

Controller:

public function destroy(Request $request): \Illuminate\Http\JsonResponse
    {
        $data = [
            'event' => Event::destroy($request->id),
            'message_id' => $request->id,
            'success' => true,
            'message' => 'Event deleted successfully'
        ];
        return response()->json($data);
    }

The form:

<form method="POST" action="{{ route('calendar.destroy') }}" class="needs-validation" novalidate=""></p>
                        @csrf
                        @method('DELETE')
                        <input type="hidden" name="id" id="id" value="">
                        <div class="form-group">
                            <x-button tabindex="3">
                                {{ __('Delete event') }}
                            </x-button>
                        </div>
                    </form>

Calendar:

eventClick: function(calendar, jsEvent, view) {
                    $('#eventDetail').modal("show");
                    $('#id').html(calendar.event.id).attr('value', calendar.event.id);
                    $('#title').html(calendar.event.title);
                    $('#start').html(moment(calendar.event.start).format('DD.MM.YYYY'));
                    $('#volunteer').html(calendar.event.extendedProps.volunteer);
                    }

Thank you @vincent15000 for your ideas :)

Please or to participate in this conversation.