1

I am trying to modify the database with ajax, but when I send the information it gives me the following error in the insertion, as if the request were empty:

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)

this is the controller:

 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:

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
  • as per error you are sending date as null.dd($request->all() and post result Commented Oct 3, 2017 at 18:10

1 Answer 1

1

Looks like you could have an error in the $.ajax, the data field is currently date, it should be:

$.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');
    }

});
Sign up to request clarification or add additional context in comments.

1 Comment

Happens to all of us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.