Default value persistence in Filament Repeater after migration rollback
0 reputation · 10 Jan 2024, 05:27 UTC
0 reputation · 10 Jan 2024, 05:27 UTC
A Filament form uses a Repeater field with a default closure that pre‑populates a row. After upgrading the application, the database migration that creates the repeater’s underlying table is rolled back due to a failure. The form still renders, but no rows appear, suggesting the default closure is not re‑executed.
• Filament 2.x or newer, where the lazy option defers rendering until interaction.
• The default closure runs only once at component initialization.
• Laravel migration rollback does not automatically reseed repeater data.
default closure after a migration rollback, or must developers manually reseed the repeater data?lazy rendering option affect the visibility of default rows during a rollback scenario?Filament does not automatically re-execute the default closure after a migration rollback. The closure runs only once during the component's initial mount. If a rollback drops the underlying table, the form will render without rows because the state created by the initial closure is gone and the trigger for a new one has already passed.
default closure is designed for initial state setup, not for maintaining data integrity across database schema changes.migrate:rollback removes the database structure, but since the Filament component is already initialized in the application lifecycle, it does not "reset" to trigger the default closure again.lazy option is enabled, the repeater's content is deferred. This does not fix the missing data; it simply masks the absence of default rows until the user interacts with the field.To ensure default rows persist or are recreated after a rollback, you must implement manual reseeding logic. The most reliable approach is overriding the mount method of the Livewire component or using a lifecycle hook to verify data existence.
mount method to check for the existence of the table or the required default records.firstOrCreate or a similar method to avoid duplicating rows if the migration is re-run.public function mount()
{
if (!Schema::hasTable('repeater_table_name')) {
// Handle missing table scenario
return;
}
if (YourModel::count() === 0) {
YourModel::create(['column' => 'default_value']);
}
}
To verify this behavior in a development environment:
default() closure.php artisan migrate, then php artisan migrate:rollback.mount logic above and refresh again to confirm the row is restored.Does the rollback drop the entire table, or does it only remove specific columns? If the table remains but is simply emptied, a simple exists() check is sufficient; if the table is dropped, the Schema::hasTable check is required to prevent SQL errors during the mount process.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.