I'm using Jquery 3, and Laravel 5.8, and want to customize an error with JSON response from My controller.
I've tried to return response from controller, but it's appear default error message.
public function destroy($id)
{
$po = Po::findOrFail($id);
$po->delete();
if($po) {
return response()->json([
'message' => 'Product Owner Deleted',
'data' => $po
]);
} else {
return response()->json([
'msg' => 'Failed to delete a product owner / Po is assigned to project'
]);
}
}
Jquery
// Delete Data
$('body').on('click', '#btn-destroy', function(event) {
event.preventDefault();
let me = $(this),
url = me.attr('href'),
csrf_token = $('meta[name=csrf-token]').attr('content');
Swal.fire({
title: 'Are you sure want to delete?',
text: "Choose Option Wisely",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, Delete it!'
}).then((result) => {
if (result.value) {
$.ajax({
url: url,
method: 'POST',
data : {
'_token': csrf_token,
'_method': 'DELETE'
},
beforeSend: function () {
toastr.warning('Deleting Data...', 'WARNING');
},
success: function(data) {
toastr.success(data.message, 'SUCCESS');
$('#datatable').DataTable().ajax.reload();
},
error: function(xhr) {
let error = xhr.responseJSON;
toastr.error(error.msg, 'ERROR');
}
});
}
});
});
I want to return error like in condition, if true show this, if false show that. But, it always return error like :
" SQLSTATE[23000]: Integrity constraint violation: 1…(id)) (SQL: delete fromposwhereid` = 1 "