I'm using laravel 5.2, Jquery 1.10 and MYSQL 5.7.
I'm using RESTful services in laravel for my message model.
I am trying to render a list of messages in a Jquery datatable and provide a delete button on each row which when pressed should fire the destroy method on MessageController.php, deleting the row then re-loading the message index page to display the updated datatable.
The datatable is being generated correctly initially but the delete button does not fire the delete method on the controller. I'm assuming my ajax code for generating the delete button must be wrong.
Thanks in advance for your help.
My datatable ajax script is-
$(document).ready( function () {
$('#message_table').DataTable({
"searchable": false,
"ajax": {
"url": "/api/message",
"type": "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
},
"columns": [
{ "data": "id"},
{ "data": "subject",
"render": function(data,type,row,meta) {
return '<a href="/message/'+row.id+'">'+data+'</a>';
}
},
{ "data": "created_at",
"render": function ( data, type, full, meta ) {
// instantiate a moment object and hand it the string date
var d = moment(data);
var month = d.month() +1 < 10 ? "0" + (d.month() +1) : d.month() +1;
var day = d.date() < 10 ? "0" + (d.date()): d.date();
return month + "/" + day + "/" + d.year();
}
},
{"defaultContent": "null", "render": function(data,type,row,meta) {
return '<button action="' + $(location).attr('protocol') + $(location).attr('host') + '/message/'+row.id+ '"' + 'name="_method"' + 'method="post"' +'type="submit"' + 'value="Delete"'+'>'+ 'Delete</button>';
}
}
]
});
} );
The generated HTML for the delete button from the last function is-
<button action="http:localhost:8000/message/6" name="_method" method="post" type="submit" value="Delete">Delete</button>
Message controller delete method-
public function destroy($id)
{
Message::destroy($id);
return Redirect::route('message.index');
}