mardi 3 octobre 2017

Empty request with update Ajax Laravel

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

        });
    });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire