Home > Software engineering >  Call to a member function update() on null with array/checkbox/edit
Call to a member function update() on null with array/checkbox/edit

Time:06-25

Need help here i m trying to take values from checkbox s and put them into a edit to change the value "Estado" to "Expedido" but when I run the code it gives me the error"Call to a member function update() on null".I also tried find and change the default of find() to the column i want and also tried with raw postgresql code.

        $chassis= $request->chassis;
    $escala = $request->escala;
    if(Auth::check() == true)
    {
       
        foreach($chassis as $chassis)
        {
            $edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->first()->update(['Estado' =>'Expedido']);

        }

CodePudding user response:

The method update(Array) doesnt exist on a model instance, it only exists on a query builder as a Builder instance.

either remove the first() call to call update on the query builder

$edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->update(['Estado' =>'Expedido']);

Or update the on the model then call save()

$edit = expedicoes::whereIn('Chassis', explode(',',$chassis))->first();
if ($edit) {
    $edit->Estado = 'Expedido';
    $edit->save();
}

I suggest you remove the update call from the foreach loop, gather all "chassis" and run a single query

$extractedChassis = [];
foreach($chassis as $chassi)
{
    $extractedChassis = array_merge($extractedChassis , explode(',',$chassi));
}
expedicoes::whereIn('Chassis', $extractedChassis)->update(['Estado' =>'Expedido']);

CodePudding user response:

this code return null

expedicoes::whereIn('Chassis', explode(',',$chassis))->first()

so you have no items that Chassis in explode(',',$chassis)

to solve this you can check if its not return null then update it

 $expedicoes = expedicoes::whereIn('Chassis', explode(',',$chassis))->first();
if(expedicoes ){
expedicoes ->update(['Estado' =>'Expedido']);
}

but i think your problem in column name so try to use chassis instead of Chassis and estado instead of Estado

  • Related