thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Translation error #5587

Open Stelikas opened 2 years ago

Stelikas commented 2 years ago

Laravel version

5.8

PHP version

7.1

Voyager version

1.2

Database

MySQL 5.7

Description

It displays an error on post request when trying to translate a model.

Steps to reproduce

  1. Create an AJAX post request to update a model
  2. Get translated model in controller
  3. Translate/update the model in controller but leave one field empty
  4. Save
  5. Get error for a field that it is not required in the database

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'value' cannot be null (SQL: update translations set value = ?, translations.updated_at = 2022-04-26 16:43:22 where id = 1800)"

Expected behavior

Even though the data is saved, it returns an error on AJAX post request.

Screenshots

No response

Additional context

No response

Stelikas commented 2 years ago

Just writing the sample code here:

        $service = Service::find($id);
        $locale = app()->getLocale();
        $service = $service->translate($locale);

        $service->category_id = $data['service']['category_id'];
        $service->related_articles = $data['service']['related_articles'];
        $service->title = $data['service']['title'];
        $service->slug = $data['service']['slug'];
        $service->introtext = $data['service']['introtext'];
        $service->image = $data['service']['image'];
        $service->author = $data['service']['author'];
        $service->approved_by = $data['service']['approved_by'];
        $service->average_cost = $data['service']['average_cost'];
        $service->duration = $data['service']['duration'];
        $service->frequency = $data['service']['frequency'];
        $service->meta_title = $data['service']['meta_title'];
        $service->meta_description = $data['service']['meta_description'];
        $service->published = $data['service']['published'];
        $service->inherit_pricetable = $data['service']['inherit_pricetable'];
        $service->calendar_service = $data['service']['calendar_service'];

        $service->save();

Also, i just noticed it doesn't save all of the fields, in the database the only field that is required is the title which is filled but it displays the error i wrote above at the issue, also some of the fields above are not translatable but i don't think that's an issue, based on the documentation if the field is not translatable it will make changes directly to the model itself.

In voyager admin it works fine, but I'm creating a custom admin dashboard and the above code doesn't work for some reason.

Stelikas commented 2 years ago

Update

Looks like because some of the fields were null the error was displayed, although they are not required in the database, Voyager returns this error without displaying for which field it is talking about, still it does not update non translatable fields like the documentation says that it will make changes directly to the model itself, i hope someone knows something on this topic because i will have to do a lot of writing for such a simple thing(Get translated model->save translated model with translations->get non translatable model->save non translatable fields).