Attempt to assign property

1

Friends, you could guide me with this problem that I have, I am receiving an array, but when I want to update the data to my base it throws me the following error:

  

Attempt to assign property 'stock' of non-object

My code is as follows:

if($det=$request->detalles) {
    foreach ($det as $detalle=> $valor) {
        $detalle=Detalle::where('art_id', '=', $idArt)
            ->where('codigo_id', '=', $valor)
            ->exists();
    }
}

if($detalle==""){
    $doNeg=false;
    $errmesg="No existe articulo";
}

$detalle->stock=1;
$detalle->updated_at = Carbon::now();
if (Auth::guard('supervisor')->check()) {
    $detalle->admin_id= Auth::id();
} elseif (Auth::guard('bodeguero')->check()) {
    $detalle->admin_id= Auth::id();
}
$detalle->save();

My problem is when you pressed the save button and it tells me that Attempt to assign property of non-object , porfis help, because I do not know where I am falling to update my data

    
asked by Karli 02.08.2018 в 18:32
source

1 answer

2

When you are calling $detalle->stock , $ detail does not exist (unless you are omitting any part of the code).

In your cycle:

if($det=$request->detalles) {
    foreach ($det as $detalle=> $valor) {
        $detalle=Detalle::where('art_id', '=', $idArt)
            ->where('codigo_id', '=', $valor)
            ->exists();
    }
}

The only thing you do is verify if there is a Detail that meets the requested conditions and save it in $ detail as true or false, because exists () returns true or false strong>.

If you want to instantiate your $ detail with a Detail type , you would have to do:

$detalle=Detalle::where('art_id', '=', $idArt)
                ->where('codigo_id', '=', $valor)
                ->first();

This will return a Detail instance or NULL if it is not found.

But still, as you do within a foreach, you will also lose the value of $ detail at the end of the cycle . You should call some function within the same cycle after instantiating $ detail. For example:

if($det=$request->detalles) {
    foreach ($det as $detalle=> $valor) {
        $detalle=Detalle::where('art_id', '=', $idArt)
            ->where('codigo_id', '=', $valor)
            ->first();
        if($detalle) {
            self::hacerLoQueTienesQueHacer($detalle);
        }
    }
}
public static function hacerLoQueTienesQueHacer($detalle)
{
    $detalle->stock=1;
    $detalle->updated_at = Carbon::now();
    if (Auth::guard('supervisor')->check()) {
        $detalle->admin_id= Auth::id();
    } elseif (Auth::guard('bodeguero')->check()) {
        $detalle->admin_id= Auth::id();
    }
    $detalle->save();
}

I created a static function because I have no idea where you are standing in your code, but if what you want to do is a common detail task, the ideal would be for the Detail class to have a function called Do What You Have to Do and in the cycle you would simply do :

if($det=$request->detalles) {
    foreach ($det as $detalle=> $valor) {
        $detalle=Detalle::where('art_id', '=', $idArt)
            ->where('codigo_id', '=', $valor)
            ->first();
        if($detalle) {
            $detalle->hacerLoQueTienesQueHacer();
        }
    }
}
    
answered by 02.08.2018 / 19:38
source