kongulov / nova-tab-translatable

This package contains a NovaTabTranslatable class you can use to make any Nova field type translatable with tabs.
MIT License
79 stars 17 forks source link

MorphOne storing doesn't work #22

Closed mgralikowski closed 2 years ago

mgralikowski commented 2 years ago

Nova 4.0.

MorphOne field works in 50%: MorphOne::make('Seo', null, Seo::class),

image

translations are displayed correctly in all tabs, but when we try to store form we will lose all translations (null value in JSON). I dumped the request and I see all translations, the only difference is that they send as a subarray. Updating a related model as a separate resource works.

Is someone who resolved this issue?

kongulov commented 2 years ago

@mgralikowski Can you send the full code usage along with the package? so i can recreate your problem and solve it

mgralikowski commented 2 years ago

Thanks for the answer. Unfortunately, I can't, but it should be easy to recreate in any project.

Let's assume we have two models Page and Seo.

Seo is a morph model:

public function seoable(): MorphTo
{
    return $this->morphTo();
}

The resource class of SEO:

return [
    ID::make()->sortable(),
    NovaTabTranslatable::make([
        Text::make('title')->sortable(),
    ]),

    MorphTo::make('Seoable')->types([
        Page::class,
    ]),
];

And it works, we can edit this page using the standard edit form.

Now we want to add SEO directly to the Page resource form.

return [
    ID::make()->sortable(),
    Text::make('Title')->sortable(),
    MorphOne::make('Seo', null, Seo::class),
];

So we have two sections - fields that belong to the base Resource and a section with Seo (my screenshot). If we don't use NovaTabTranslatable it works - it saves en (default) translation. Next, we wish to add your "tabs", and the form works - is populated correctly using translations from DB. The problem is only during submitting the form - translations will be gone. Laravel nova sends POST requests with translations (translations_attribute_locale) in a sub-array (seo => []), and it seems ok at this point.

kongulov commented 2 years ago

@mgralikowski

Thank you very much for finding the problem. Problem solved, please update to version 2.0.5

mgralikowski commented 2 years ago

Thanks a lot. It works like a charm now.