F
Filament9mo ago
V01D

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'),]),
No description
27 Replies
cheesegrits
cheesegrits9mo ago
You aren't doing anything with the filled form.
Action::make('modal_edit_with_data')
//
->action(function (Model $record, array $data) {
// do whatever you need to do with the form data
}
)
Action::make('modal_edit_with_data')
//
->action(function (Model $record, array $data) {
// do whatever you need to do with the form data
}
)
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.
V01D
V01D9mo ago
I added this: ->action(function (array $data): void { $this->record->save(); }) but it still doesn't work.
Vp
Vp9mo ago
->action(function (array $data): void {
Model::create($data);
})
->action(function (array $data): void {
Model::create($data);
})
cheesegrits
cheesegrits9mo ago
What about explaining what you are actually trying to achieve?
V01D
V01D9mo ago
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
cheesegrits
cheesegrits9mo ago
What normal way have you tried?
V01D
V01D9mo ago
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
cheesegrits
cheesegrits9mo ago
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.
V01D
V01D9mo ago
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
No description
cheesegrits
cheesegrits9mo ago
OK, so now paste the 'bibliography' relationship from your main model.
V01D
V01D9mo ago
sorry i dont get it
cheesegrits
cheesegrits9mo ago
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 relationship
}
public function bibliography()
{
// return relationship
}
V01D
V01D9mo ago
public function bibliography() { return $this->belongsTo(Bibliography::class); } oh yes, but where do I paste it?
cheesegrits
cheesegrits9mo ago
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.
V01D
V01D9mo ago
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;
cheesegrits
cheesegrits9mo ago
OK, so what's the relationship (in Laravel terms) between bibliography and bibliography_translation? Is it a HasMany or a MorphMany?
V01D
V01D9mo ago
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; }
cheesegrits
cheesegrits9mo ago
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.
V01D
V01D9mo ago
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'); } }
cheesegrits
cheesegrits9mo ago
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.
V01D
V01D9mo ago
yes that is
cheesegrits
cheesegrits9mo ago
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.
cheesegrits
cheesegrits9mo ago
OK, so your relationship "chain" from your ProductDetail model is bibliography -> translations. I've never tried nested relationships in Filament, but you could try ...
Group::make()
->relationship('bibliography')
->schema([
Repeater::make('translations')
->relationship('translations')
->schema([
TextInput::make('locale'),
Textarea::make('text'),
]),
]),
Group::make()
->relationship('bibliography')
->schema([
Repeater::make('translations')
->relationship('translations')
->schema([
TextInput::make('locale'),
Textarea::make('text'),
]),
]),
See if that works.
V01D
V01D9mo ago
OMG it worked! thanks man!
cheesegrits
cheesegrits9mo ago
Filament is literally black magic voodoo, you just have to figure out the right incantations.
V01D
V01D9mo ago
so it's not psychic but it's magical xD
cheesegrits
cheesegrits9mo ago
It is close to becoming sentient.
Want results from more Discord servers?
Add your server
More Posts
Bulk action to Get Selected Material and Create New Order With These MaterialsHello, I created this bulk action to get material selected and pass to a new order: `Tables\ActionEdit only single record with custom pagehi! im newbie in filament and php(laravel), so my question is next: can you explain to me please howHow to modify a model before form fill?Hello. I have a laravel form I'm trying to convert into Filament. Before loading the create page I wRichEditor view source option?I don't see any options with the RichEditor's toolbar buttons options to allow you to view the raw hInclude prefix as part of text input.How can I do to include the prefix on `TextInput` for saving in database? My code: ```php TextInput:How can I work around this `column reference "id" is ambiguous`?```php // GradeResource.php public static function getEloquentQuery(): Builder { $query = parenHow can load an external video from Youtube, Vimeo?Hello guys, I want add an input in my form and save a link from youtube, vimeo or another site, how Inject Model in Livewire ComponentI'm trying to inject my campaign model in a livewire component through form. I tried the instructionshow hidden fields conditionaly not workingas title says ```php Forms\Components\Checkbox::make('availability')->default(true), Forms\Componenhello. I have this problem when creating a category from the admin panel. I did everything well. buhello. I have this problem when creating a category from the admin panel. I did everything well. but