#Observer causing update form to get stuck

8 messages · Page 1 of 1 (latest)

errant crag
#

Any idea what's causing it?
This is my observer

public function updated(StartRonde $startRonde): void
    {
        // Check if scans are not empty
        if (!empty($startRonde->scans)) {
            // Count the number of references in scans
            $scanCount = count($startRonde->scans);

            // Fetch the associated ronde and its pointeaux
            $ronde = $startRonde->ronde; // Assuming you have a relationship defined
            $pointeauCount = $ronde->pointeaux()->count(); // Count the pointeaux

            // Check if the counts are equal
            if ($scanCount === $pointeauCount) {
                // Add your logic here, for example, log a message or update a field

                $startRonde->status = 'finished';
                $startRonde->save(); // Save the changes to the database

            } else {
                // Logic for when counts do not match (optional)
                Log::warning('Reference count does not match the number of Pointeaux for StartRonde ID: ' . $startRonde->id);
            }
        }
    }
quaint lodgeBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

high hare
#

I refactored your code with return early. Maybe you can find your problem more easily:

public function updated(StartRonde $startRonde): void
{
    // Check if scans are empty
    if (empty($startRonde->scans)) {
        return;
    }

    // Count the number of references in scans
    $scanCount = count($startRonde->scans);

    // Fetch the associated ronde and its pointeaux
    $ronde = $startRonde->ronde; // Assuming you have a relationship defined
    $pointeauCount = $ronde->pointeaux()->count(); // Count the pointeaux

    // Check if the counts are not equal
    if ($scanCount !== $pointeauCount) {
        Log::warning('Reference count does not match the number of Pointeaux for StartRonde ID: ' . $startRonde->id);
        return;
    }

    // Logic when counts are equal
    $startRonde->status = 'finished';
    $startRonde->save(); // Save the changes to the database
}
errant crag
upbeat portal
#

If you're saving the same model that the observer is using you'll get a loop. Try to change $startRonde->save() to $startRonde->saveQuietly() so that it doesnt trigger events

errant crag
#

Ah alright, thanks a lot

#

Works brother, thanks a ton. Makes total sense

quaint lodgeBOT