Home > database >  Ajax request giving id NULL
Ajax request giving id NULL

Time:02-10

Good Morning...I am trying to update the data in table using ajax, while passing the data to controller using id, ID preview is showing NULL and so not able to update the data in Table. I am trying to add new data in one table and at same time update data in another table using same ajax... Ajax -

$.ajaxSetup({

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }

});

$(document).on('click', '#footer_addnewinspectiondata_button', function() {

    $.ajax({

        type: 'put',
        url: '/addbuffaloinspectiondata',
        dataType: 'json',
        data: {
            'buffaloID': $('#buffaloID').val(),
            'inspectiondate': $('#inspectiondate').val(),
            'pregnantstatus': $('#pregnantstatus').val(),
            'healthstatus': $('#healthstatus').val(),
            'weight': $('#weight').val(),
            'height': $('#height').val(),
            'weeklytika': $('#weeklytika').val(),
            'monthlytika': $('#monthlytika').val(),
            'inspection': $('#inspection').val(),
            'inspectionby': $('#inspectionby').val(),
            'inspectionnote': $('#inspectionnote').val(),

        },
        success: function(data) {

            console.log(data);

        },
        error: function(e) {
            console.log(e);
        }
    });
});

Web.php File

Route:: put('/addbuffaloinspectiondata', 
       [viewbuffaloController::class,'addbuffaloinspectiondata'])->name('addbuffaloinspectiondata');

Controller File -

public function addbuffaloinspectiondata(Request $req) 
{
    // add new data in buffaloinspection table for Buffalo ID

    $newdata = new buffaloinspectiondata;

    $newdata->buffaloID            = $req->buffaloID;
    $newdata->inspectiondate       = $req->inspectiondate;
   
    $newdata->weight               = $req->weight;
    $newdata->height               = $req->height;
    $newdata->pregnant             = $req->pregnantstatus;
    
    $newdata->health               = $req->healthstatus;
    $newdata->weeklytika           = $req->weeklytika;
    $newdata->monthlytika          = $req->monthlytika;
    
    $newdata->inspection           = $req->inspection;
    $newdata->inspectionby         = $req->inspectionby;
    $newdata->inspectionnote       = $req->inspectionnote;

    // update data in BuffaloDATA Table

    $updatedata = buffalodata::find($req->buffaloID);
    
        $updatedata->weight             = $req->get('weight');
        $updatedata->height             = $req->get('height');
    

        $newdata->save ();
        $updatedata->save ();

        return response()->json([$newdata,$updatedata],200);
}

Did i miss something or doing wrong in code... Thanks in Advance.

enter image description here

enter image description here enter image description here

CodePudding user response:

The Reason that you are getting Id NULL is dd($updatedata); it will stop execution after that remove it

and change your return like this

return response()->json([$newdata,$updatedata],200);

your controller

public function addbuffaloinspectiondata(Request $req) 
{
    // add new data in buffaloinspection table for Buffalo ID

    $newdata = new buffaloinspectiondata();

    $newdata->buffaloID            = $req->buffaloID;
    $newdata->inspectiondate       = $req->inspectiondate;
   
    $newdata->weight               = $req->weight;
    $newdata->height               = $req->height;
    $newdata->pregnant             = $req->pregnantstatus;
    
    $newdata->health               = $req->healthstatus;
    $newdata->weeklytika           = $req->weeklytika;
    $newdata->monthlytika          = $req->monthlytika;
    
    $newdata->inspection           = $req->inspection;
    $newdata->inspectionby         = $req->inspectionby;
    $newdata->inspectionnote       = $req->inspectionnote;
    $newdata->save();

    // update data in BuffaloDATA Table

    $updatedata = buffalodata::find($req->buffaloID);
    $updatedata->weight  = $req->weight;
    $updatedata->height  = $req->height;
    $updatedata->update();

        
        

        return response()->json([$newdata,$updatedata],200);
}

CodePudding user response:

Try this

public function addbuffaloinspectiondata(Request $req)     
{
    // add or update operation.

    if(isset($req->buffaloID)){

        //buffalodata model
        $newdata = buffaloinspectiondata::find($req->buffaloID);

        //buffalodata model
        $updatedata = buffalodata::find($req->buffaloID);

        $updatedata->weight             = isset($req->get('weight'))? $req->get('weight') : 0 ;
        $updatedata->height             = isset($req->get('weight'))? $req->get('height') : 0 ;
        $updatedata->save ();

    }else{
        $newdata = new buffaloinspectiondata;
    }

    $newdata->buffaloID            = $req->buffaloID;
    $newdata->inspectiondate       = $req->inspectiondate;
    $newdata->weight               = isset($req->get('weight'))? $req->get('weight') : 0 ;
    $newdata->height               = isset($req->get('weight'))? $req->get('height') : 0 ;
    $newdata->pregnant             = $req->pregnantstatus;
    $newdata->health               = $req->healthstatus;
    $newdata->weeklytika           = $req->weeklytika;
    $newdata->monthlytika          = $req->monthlytika;
    $newdata->inspection           = $req->inspection;
    $newdata->inspectionby         = $req->inspectionby;
    $newdata->inspectionnote       = $req->inspectionnote;
    $newdata->save();

    return response()->json([$newdata,$updatedata],200);
}
  • Related