0

I have this function to update the DB values, but one of the statements is not working. I created a relation to the table called ventas, however, it is not updating it´s value to NULL. This is my code:

public function cambiarEstatusFactura( Request $request){
    try{
        \DB::beginTransaction();

        $factura = FacturaVenta::with('venta')->where( 'id' , $request->id )->first();

        // dd('Esto : ' , $factura->venta->factura_uuid);

        if( $factura->estatus == 'Timbrada' ){
            $factura->estatus = 'Cancelada';
            $factura->uuid = NULL;
            $factura->venta->factura_uuid = NULL;
            $factura->save();
        }

        \DB::commit();

    }catch (Exception $e){
        \Alert::error($e->getMessage())->flash();
        return redirect('admin/finanzas/factura/venta');
    }
}

The statement that is not working is:

$factura->venta->factura_uuid = NULL;

Though the other statements work seamlessly, this seems to have issues but I do not get any error messages.

Variable $factura contains FacturaVenta model and the relation venta brings the model from the table ventas, that's why I try to access to it via $factura->venta and the commented dd('Esto : ' , $factura->venta->factura_uuid); in the code brings the value correctly, that means that it's pointing correctly to the correct table but it's not being updated to NULL.

I just want to update the value from whatever it has to NULL.

Could you help me please?

Rodri6uez
  • 399
  • 1
  • 5
  • 14
  • 1
    You have to save a related model separately (`$factura->venta->save()`). Laravel wont loop through every relationship on your model and check to see if they need saved as well. – Brian Thompson Feb 24 '22 at 21:30
  • Thanks a lot man! That was the problem. I really appreciate it. – Rodri6uez Feb 24 '22 at 21:39

0 Answers0