Skip to content

Massively update doesnt update default locale when model has early load relation 'translation' #215

Open
@p-andrey

Description

@p-andrey

Describe the bug
If you specify in the model $with = ['translation'] (not 'translations'!) and massively update the model with translations (using update method), then the fields with the default locale are not updated.

If you prescribe $with = ['translations'] everything works correctly.

To Reproduce
Add to Country model (in tests directory)

/**
 * The relations to eager load on every query.
 *
 * @var array
 */
protected $with = ['translation'];

Then create test

/** @test */
public function it_update_all_translated_locales_when_translation_relation_is_loaded(): void
{
    $this->app->make('config')->set('translatable.locales', ['de', 'en']);
    $this->app->setLocale('de');
    $this->app->make(Locales::class)->load();

    // First create country
    Country::create([
        'id' => 100,
        'code' => 'my',
        'de' => [
            'name' => 'Deutschland',
        ],
        'en' => [
            'name' => 'Germany',
        ],
    ]);

    $country = Country::find(100);

    // try mass update
    $country->update([
        'code' => 'my',
        'de' => [
            'name' => 'New Deutschland',
        ],
        'en' => [
            'name' => 'New Germany',
        ],
    ]);

    $country = Country::find(100);

    static::assertEquals(100, $country->getKey());
    static::assertEquals('New Deutschland', $country->getTranslation('de', false)->name);
    static::assertEquals('New Germany', $country->getTranslation('en', false)->name);
}

The Test was failed with result:

Failed asserting that two strings are equal.
Expected :'New Deutschland'
Actual   :'Deutschland'

Expected behavior
In spite of the fact that early loading is specified in the model $with = ['translation'] - the bulk update must take place completely (for all languages).

Versions (please complete the following information)

  • PHP: 7.4.16
  • Database: MySQL Ver 8.0.23
  • Laravel: v7.30.4
  • Package: v11.9.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions