PHP Snippet 1:
$.ajax({
method: 'POST',
url: urlDeleteComment,
data: {
commentId: commentId,
_method: 'DELETE',
_token: token
}
}).done(function(response) {
})
PHP Snippet 2:
<form method='POST' id='delete_comment'>
<input type="hidden" name="comment_id" value="{{ $comment->id }}">
@csrf
<div id="status" style="display: none;"></div>
<button class='submit-btn delete-comment' type='submit' name='commentDelete'>X</button>
</form>
PHP Snippet 3:
// POST
Route::post('/comment/delete', 'CommentsController@deleteComment')->name('deleteComment');
PHP Snippet 4:
<script type="text/javascript">
$(document).ready(function(){
$('#delete_comment').on('submit', function(event){
event.preventDefault();
// ADD WAIT CSS IF YOU WANT HERE :)
$.ajax({
url:"{{ route('deleteComment') }}",
method:"POST",
data: new FormData(this),
// DATA RETURN JSON
dataType:'JSON',
contentType: false,
cache: false,
processData: false,
success:function(data)
{
// REMOVE YOUR WAIT CSS BEFORE SHOW YOUR SUCCESS MSG
// SHOW SUCCESS OR WARNING MSG
jQuery('#status').toggle('show');
$('#status').addClass(data.status);
$('#status').innerHTML = data.msg;
},
error: function(data) {
console.log(data);
}
});
});
});
</script>
PHP Snippet 5:
<?php
protected function deleteComment(Request $request){
// NOT GIVE ANY ONE TO DELETE YOUR COMMNET
$comment = Comment::find($request->comment_id);
// CHECK IF IS COMMNET IN DATABASE :)
if ($comment) {
$comment->delete();
return response()->json([
'msg' => 'success',
'status' => 'alert alert-success'
]);
}
return response()->json([
'msg' => 'error, not found',
'status' => 'alert alert-danger'
]);
}
?>