Jared Eitnier Jared Eitnier - 1 year ago 105
PHP Question

Laravel redirect to route from controller when called by AJAX

Given this chunk of

that calls

url: '/dashboard/friends/' + id,
type: 'DELETE',
data: { src: 'show' },
success: function(response) {

How can I
return Redirect::route('dashboard.friends.index')
after the delete procedure is completed? I guess this is trying to return the response back to AJAX which doesn't know how to react.

I could just
window.location.href = '/dashboard/friends'
but I want to Flash a success message to the view which I can't do with AJAX.

Answer Source

Here is how I solved this issue on my own. For functions such as performing an AJAX delete of a model, it will prompt a confirmation modal, perform the delete, flash the message and redirect to the provided route.

Once setup it can be done dynamically by passing in the proper data attributes to the original delete button or link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download