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