我尝试使用laravel ofc在自己的作业中使用ajax和datatable。当我尝试编辑原始数据时,出现此错误:
PUT http://localhost:8000/categories/1 500(内部服务器错误)
有什么建议吗?
我的控制器:
public function edit($id)
{
$categories = Category::findOrFail($id);
return view('categories.form', compact('categories'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'category_name' => 'required|string|' . $id
]);
$categories = Category::findOrFail($id);
$categories->update($request->all());
}
我的App.js:
$('#modal-btn-save').click(function (event) {
event.preventDefault();
var form = $('#modal-body form'),
url = form.attr('action'),
method = $('input[name=_method]').val() == undefined ? 'POST' : 'PUT';
form.find('.help-block').remove();
form.find('.form-group').removeClass('has-error');
$.ajax({
url: url,
method: method,
data: form.serialize(),
success: function (response) {
form.trigger('reset');
$('#modal').modal('hide');
$('#datatable').DataTable().ajax.reload();
Swal.fire({
type: 'success',
title: 'Sukses !',
text: 'Data berhasil di simpan !'
});
},