F
Filament3mo ago
MZX

Observer causing update form to get stuck

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);
}
}
}
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);
}
}
}
Solution:
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
Jump to solution
5 Replies
Erlich
Erlich3mo ago
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
}
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
}
MZX
MZXOP3mo ago
The observer does work though, can see the change in the database. But the Filament Edit Form gets stuck on loading forever after clicking save
Solution
Bruno Pereira
Bruno Pereira3mo ago
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
MZX
MZXOP3mo ago
Ah alright, thanks a lot Works brother, thanks a ton. Makes total sense
MZX
MZXOP3mo ago
Thanks buddy!
Want results from more Discord servers?
Add your server