更新Ajax Laravel的空请求

时间:2017-10-03 18:08:36

标签: php jquery ajax laravel laravel-5

我正在尝试使用ajax修改数据库,但是当我发送信息时,它在插入时给出了以下错误,就像请求是空的一样:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'date' cannot be null (SQL: update `appointments` set `date` = , `startTime` = , `finalTime` = , `user_id` = , `patient_id` = , `updated_at` = 2017-10-03 18:05:40 where `id` = 32)

这是控制器:

 public function update(Request $request, $id)
{
    $appointment =Appointment::find($id);
    $appointment->patient_id = $request->patient_id;
    $appointment->user_id = $request->user_id;
    $appointment->date = $request->date;
    $appointment->startTime = $request->startTime;
    $appointment->finalTime = $request->finalTime;

    $appointment->save();
}

路线:

Route::name('appointments.update')->put('/citas/{id}', 'AppointmentController@update'); 

AJAX:

$('#update').on('click', function(){
        var x = $(this);
        var update_url = x.attr('data-href')+'/'+x.attr('data-id');

        var user_id = $('select[name="user_id"').val();
        var patient_id = $('select[name="patient_id"').val();
        var date = $('input#date').val();
        var startTime = $('input#startTime').val();
        var finalTime = $('input#finalTime').val();
        console.log(date);
        console.log(startTime);
        console.log(finalTime);
        $.ajax({
            url: update_url,
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            type:'PUT',
            date:{user_id:user_id, patient_id:patient_id, date:date, startTime:startTime, finalTime:finalTime},
            success:function(result){
                alert('success');
            },
            error:function(result){
                alert('error');
            }

        });
    });

1 个答案:

答案 0 :(得分:1)

您可能会在$.ajax中出现错误,data字段当前为date,应该是:

$.ajax({
    url: update_url,
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    type:'PUT',
    data:{user_id:user_id, patient_id:patient_id, date:date, startTime:startTime, finalTime:finalTime},
    success:function(result){
        alert('success');
    },
    error:function(result){
        alert('error');
    }

});