modal doesn't save
I have this modal it shows the content i want but doesn't save the changes:
Section::make('Bibliography') ->relationship('bibliography')
->schema([
Actions::make([ Action::make('modal_edit_with_data') ->mountUsing(function (Forms\ComponentContainer $form, Bibliography $record) { $data = []; // Get all data in array $dataRecord = $record->toArray(); // Save in $data by key = value foreach ($dataRecord as $key => $value) { $data[$key] = $value; } // Get all translations in array and save in $data by key = value foreach ($record->getTranslationsArray() as $key => $value) { $data[$key] = $value; } // Filled your data to $form and return return $form->fill($data); }) ->form([ Tabs::make('lang form') ->tabs([ Tabs\Tab::make('En') ->schema([ Forms\Components\RichEditor::make('en.text') ]), Tabs\Tab::make('Es') ->schema([ Forms\Components\RichEditor::make('es.text')
]), Tabs\Tab::make('Pt') ->schema([ Forms\Components\RichEditor::make('pt.text') ]),
]), ]),])->Columns('full'),]),
Actions::make([ Action::make('modal_edit_with_data') ->mountUsing(function (Forms\ComponentContainer $form, Bibliography $record) { $data = []; // Get all data in array $dataRecord = $record->toArray(); // Save in $data by key = value foreach ($dataRecord as $key => $value) { $data[$key] = $value; } // Get all translations in array and save in $data by key = value foreach ($record->getTranslationsArray() as $key => $value) { $data[$key] = $value; } // Filled your data to $form and return return $form->fill($data); }) ->form([ Tabs::make('lang form') ->tabs([ Tabs\Tab::make('En') ->schema([ Forms\Components\RichEditor::make('en.text') ]), Tabs\Tab::make('Es') ->schema([ Forms\Components\RichEditor::make('es.text')
]), Tabs\Tab::make('Pt') ->schema([ Forms\Components\RichEditor::make('pt.text') ]),
]), ]),])->Columns('full'),]),
27 Replies
You aren't doing anything with the filled form.
I'm not sure what you are actually trying to do, and you need to tell Filament what to do when you build a custom action. t's not psychic.
I added this:
->action(function (array $data): void {
$this->record->save();
})
but it still doesn't work.
What about explaining what you are actually trying to achieve?
The thing is that I have a resource "product detail" this product detail is associated with a "bibliography" these have translations and I need to show this information within the resourceProduct. When I try to add it in the normal way the content appears empty that's why I'm trying to load it in the modal which works but it doesn't save the changes
What normal way have you tried?
the one that appears in the documentation
Fieldset::make('Metadata')
->relationship('metadata')
->schema([
TextInput::make('title'),
Textarea::make('description'),
FileUpload::make('image'),
])
https://filamentphp.com/docs/3.x/panels/resources/relation-managers
Paste your metadata relationship from the ProductDetail (or whatever it is) model.
We need to see your actual code, not examples straight out of the docs.
Section::make('Bibliography') ->relationship('bibliography')
->schema([
Tabs::make('lang form') ->tabs([ Tabs\Tab::make('En') ->schema([ Forms\Components\RichEditor::make('en.text') ]), Tabs\Tab::make('Es') ->schema([ Forms\Components\RichEditor::make('es.text')
]), Tabs\Tab::make('Pt') ->schema([ Forms\Components\RichEditor::make('pt.text') ]), ]), ])->Columns('full'), something like that? as I said it appears empty, but may the problem is my translation package
Tabs::make('lang form') ->tabs([ Tabs\Tab::make('En') ->schema([ Forms\Components\RichEditor::make('en.text') ]), Tabs\Tab::make('Es') ->schema([ Forms\Components\RichEditor::make('es.text')
]), Tabs\Tab::make('Pt') ->schema([ Forms\Components\RichEditor::make('pt.text') ]), ]), ])->Columns('full'), something like that? as I said it appears empty, but may the problem is my translation package
OK, so now paste the 'bibliography' relationship from your main model.
sorry i dont get it
The main model that your form is on, you said ProductDetail (or something like that). That model should have a relationship on it called 'bibiography', something like ...
public function bibliography()
{
return $this->belongsTo(Bibliography::class);
}
oh yes, but where do I paste it?
Here. So I can see it.
OK. So you have a bibliography_id foreign key on your ProductDetail table, yes?
And what does the bibliography table look like. What fields does it have.
only text but its associated with a translation table
CREATE TABLE
bibliography
(
id
int(11) NOT NULL AUTO_INCREMENT,
hide_locale
varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=347 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE bibliography_translation
(
id
int(11) NOT NULL AUTO_INCREMENT,
translatable_id
int(11) NOT NULL,
text
longtext COLLATE utf8_unicode_ci NOT NULL,
locale
varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id
),
UNIQUE KEY bibliography_translation_uniq_trans
(translatable_id
,locale
),
KEY IDX_CDE13E762C2AC5D3
(translatable_id
),
CONSTRAINT FK_CDE13E762C2AC5D3
FOREIGN KEY (translatable_id
) REFERENCES bibliography
(id
) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=539 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;OK, so what's the relationship (in Laravel terms) between bibliography and bibliography_translation? Is it a HasMany or a MorphMany?
it doesn't have one tha package (astrotomic) handles it
class Bibliography extends Model implements TranslatableContract
{
use HasFactory;
use Translatable;
protected $table = 'bibliography';
protected $guarded = [];
public $translatedAttributes = ['text'];
public $timestamps = false;
}
I suspect that interface (Contract) implements a relationship. So take a look at the Translatable trait.
My bet is there a 'translations' relationship in the trait, probably MorphMany.
trait Translatable
{
// This override get translations fields
protected function fillForm(): void
{
$this->callHook('beforeFill');
$data = $this->getRecord()->attributesToArray();
foreach (static::getRecord()->getTranslationsArray() as $key => $value) {
$data[$key] = $value;
}
$data = $this->mutateFormDataBeforeFill($data);
$this->form->fill($data);
$this->callHook('afterFill');
}
// This override get SQL optimization and get all translations
protected function getTableQuery(): Builder
{
return static::getResource()::getEloquentQuery()->with('translations');
}
}
Is it this package?
https://github.com/Astrotomic/laravel-translatable
GitHub
GitHub - Astrotomic/laravel-translatable: A Laravel package for mul...
A Laravel package for multilingual models. Contribute to Astrotomic/laravel-translatable development by creating an account on GitHub.
yes that is
OK, so the Contract does implement a translations() HasMany (I was wrong, it's not a MorphMany).
https://github.com/Astrotomic/laravel-translatable/blob/main/src/Translatable/Contracts/Translatable.php#L49
GitHub
laravel-translatable/src/Translatable/Contracts/Translatable.php at...
A Laravel package for multilingual models. Contribute to Astrotomic/laravel-translatable development by creating an account on GitHub.
OK, so your relationship "chain" from your ProductDetail model is bibliography -> translations. I've never tried nested relationships in Filament, but you could try ...
See if that works.
OMG it worked!
thanks man!
Filament is literally black magic voodoo, you just have to figure out the right incantations.
so it's not psychic but it's magical xD
It is close to becoming sentient.