ronnyandre
10 months ago

Maximum function nesting level reached

Posted 10 months ago by ronnyandre

I have seen a lot of answers to this question, however, I set Xdebug max nesting level to 4096 and still get this error. Clearly there must be a bug in the code?

The error only shows when I created a model observer:

public function updated(Invoice $invoice)
    {
        $subtotal = 0;
        $discount = 0;
        $vat = 0;
        $total = 0;

        foreach ( $invoice->items as $item )
        {
            $itemVat = $item->subtotal * $item->vat;
            $itemDiscount = $item->subtotal * $item->discount;

            $subtotal += $item->subtotal;
            $discount += $itemDiscount;
            $vat += $itemVat;
            $total += $item->total;
        }

        $invoice->subtotal = $subtotal;
        $invoice->discount = $discount;
        $invoice->vat = $vat;
        $invoice->total = $total;

        $invoice->save(); // <--- This line generates the error.
    }

Any ideas where to start?

Please sign in or create an account to participate in this conversation.