我正在尝试使用laravel中的ajax更新我的数据库。当我单击按钮(切换按钮)时,它应该将数据库启用列从1更新为0.
这是在视图中编写的脚本
$(".toggle-btn").change(function() {
var id = $(this).attr('name'); // $(this) refers to button that was clicked
$.ajax({
url: '/adminpanel/dviulaan/stt',
method: "post",
data: {'id' : id} ,
dataType: "json",
});
});
这是我的路线
Route::post('adminpanel/dviulaan/stt', 'AdminDvAnnouncement@status');
这是控制器功能
public function status()
{
$id = Input::all();
if (Request::ajax()) {
DvAnnouncement::where('announcement_id', $id)->update(
[
'enable' => '0',
'user_updated' => Auth::user()->id,
'updated_at' => new DateTime,
]);
}
$response = array(
'status' => 'success',
'msg' => 'Option created successfully',
);
return Response::json( $response );
}
当我点击按钮时,它会在consol中显示以下错误
POST http://localhost/adminpanel/dviulaan/stt 500 (Internal Server Error)
请帮我找错。
我甚至改变了控制器方法,如下所示
public function status(Request $request)
{
$id = Input::get('id');
if (Request::ajax()) {
DvAnnouncement::where('announcement_id', $id)->update(
[
'enable' => '0',
'user_updated' => Auth::user()->id,
'updated_at' => new DateTime,
]);
}
$response = array(
'status' => 'success',
'msg' => 'Option created successfully',
);
return Response::json( $response );
}
答案 0 :(得分:1)
$id = Input::all();
=> $id = Input::get('id');
UPD
包括在头
<meta name="csrf-token" content="{!! csrf_token() !!}" />
然后改变功能
$(".toggle-btn").change(function() {
var id = $(this).attr('name'); // $(this) refers to button that was clicked
$.ajax({
url: '/adminpanel/dviulaan/stt',
method: "post",
data: {'id' : id, '_token': $('meta[name="csrf-token"]').attr('content')} ,
dataType: "json",
});
});