[英]How to Update AJAX Laravel 5.2
I use the following code to edit But despite the success of editing the message is not stored in the database, where is the problem? 我使用以下代码进行编辑,但是尽管编辑成功,但消息未存储在数据库中,问题出在哪里?
my controller: 我的控制器:
public function patchUpdate($id, messageRequest $request)
{
try {
$category = Category::findOrNew($id);
$category->update($request->all());
return response()->json(['sms' => 'ok']);
} catch (Exception $e) {
return $request->messages();
}
}
my Ajax code: 我的Ajax代码:
$('#submitUpdate').on('click', function (e) {
e.preventDefault();
var cat = $('#updateForm').serialize();
var id = $('#updateForm').attr('placeholder');
var token = $('input[name="_token"]').attr('value');
$.ajax({
url: "{{ url('/update') }}" + '/' + id + '',
type: 'post',
data: {"id": id, '_token': token, 'cat': cat},
success: function (data) {
alert(data.sms);
}
});
});
my Route code: 我的路线代码:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::post('update/{id}', ['as' => 'update/{id}', 'uses' => 'categoryController@patchUpdate']);
});
Check if your model has mass assignable attributes with the same name of inputs. 检查您的模型是否具有与输入名称相同的大量可分配属性。
class Categories extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.