diff --git a/app/GirlsInDigitalPage.php b/app/GirlsInDigitalPage.php index 0c1760d93..2b837d1fe 100644 --- a/app/GirlsInDigitalPage.php +++ b/app/GirlsInDigitalPage.php @@ -77,6 +77,31 @@ public function setAttribute($key, $value) return parent::setAttribute($key, $value); } + /** + * Used by Nova afterSave; not persisted to DB. + * + * @var array>|null + */ + public $nonPersistedButtonUpdates; + + /** + * Keep _button_updates out of the SQL UPDATE (used by Nova to pass button data to afterSave). + */ + protected static function booted(): void + { + static::saving(function (self $model): void { + $model->preserveButtonUpdatesBeforeSave(); + }); + } + + private function preserveButtonUpdatesBeforeSave(): void + { + if (array_key_exists('_button_updates', $this->attributes)) { + $this->nonPersistedButtonUpdates = $this->attributes['_button_updates']; + unset($this->attributes['_button_updates']); + } + } + public function buttons() { return $this->hasMany(GirlsInDigitalButton::class, 'page_id')->orderBy('position'); diff --git a/app/Nova/GirlsInDigitalPage.php b/app/Nova/GirlsInDigitalPage.php index 3778dd9f4..66223c511 100644 --- a/app/Nova/GirlsInDigitalPage.php +++ b/app/Nova/GirlsInDigitalPage.php @@ -356,7 +356,7 @@ public static function afterSave(NovaRequest $request, $model): void if (! Schema::hasTable('girls_in_digital_buttons')) { return; } - $updates = $model->_button_updates ?? null; + $updates = $model->nonPersistedButtonUpdates ?? $model->_button_updates ?? null; if (! is_array($updates) || empty($updates)) { return; } @@ -380,7 +380,7 @@ public static function afterSave(NovaRequest $request, $model): void report($e); } } - unset($model->_button_updates); + unset($model->_button_updates, $model->nonPersistedButtonUpdates); } catch (\Throwable $e) { report($e); }