Laravel + jquery dialog redirect after post (+fullcalendar)

I have a jquery dialog that has its’ events dynamically altered when clicking on a single event in a fullcalendar instance like so:

$('#calendar').fullCalendar({
            //calendar options here              
                    events: 'getlogs',                        
                    eventClick: function(calEvent, jsEvent, view) {
                        //POPUPCODE START
                        $.ajax({
                           url: 'getlog',
                           data: { id: calEvent.id},
                           dataType: "json",
                           success: function(data)
                           {
                               //set your values in the edit fields here
                           }
                       });
                        $('#logid').val(calEvent.id);                       
                        $("#editdeletediv").load().dialog(
                        {   //Set options for the dialog here
                                title: 'Edit/Delete log',
                                modal: true,
                                autoResize:true,
                                maxWidth: 600,
                                minWidth: 500,
                                buttons: {
                                    Delete: function(){
                                        $.ajax({
                                        url: 'removelog',
                                        type: "POST",
                                        data: { id: calEvent.id },
                                        dataType: "json"                           
                                        });                                        
                                    },
                                    Save: function(){
                                    }
                                }
                        });

                        //POPUPCODE END

                    }
            });

Then I have a route in my laravel routes.php that routes the post I do with the delete button to the correct controller like so:

Route::post('removelog/','CalendarController@removeLog');

The function in the controller is like this:

public function removeLog(){            
            $id = Input::get('id');
            DB::table('time_logs')->where('id', '=', $id)->delete();
        }

Now, this all works perfectly, there is however one downside. When I click the delete button on the dialog, it doesn’t close the dialog (I know how to do this no worries) but when I do add the functionality to close the dialog it will still show the event I previously deleted. Therefore I want to refresh my page (because when I do that the event is gone seeing as they are rendered from my “getlogs” and read straight from the database) so it will no longer show the event that has already been deleted, I tried a couple of things to get this to work:

In the jquery dialog function for the delete button I added

 window.location.reload and later location.reload

This does reload my page however, it somehow breaks off my post to the remove function.

In my controller I added:

return Redirect::route('calendar');

to return to the calendar page, this didn’t redirect me but the post still works so at least it didn’t break anything

I also tried:

return Redirect::back();

seeing as this worked for my other post where I create a log (difference here was that that post was created using a form submit), this didn’t work either

I am wondering if anyone has any ideas of how I can make this happen with the current setup I have without changing too much of the code (it’s taken me ages to get everything to work and document how I did it and I wouldn’t want to rewrite any of that)


Source: laravel

Leave a Reply