Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrapped mass assignment with unguarded models #431

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public function fill(array $attributes)
foreach ($attributes as $key => $values) {
if ($this->isWrapperAttribute($key)) {
$this->fill($values);

unset($attributes[$key]);
continue;
}
if (
$this->getLocalesHelper()->has($key)
Expand Down
26 changes: 26 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ public function it_creates_translations_using_wrapped_mass_assignment_and_locale
self::assertEquals('Pois', $vegetable->translate('fr')->name);
}

#[Test]
public function it_creates_translations_using_wrapped_mass_assignment_and_locales_with_an_unguarded_model(): void
{
$this->app->make('config')->set('translatable.translations_wrapper', '_translation_wrapper');

Vegetable::unguard();

$vegetable = Vegetable::create([
'quantity' => 5,
'_translation_wrapper' => [
'en' => ['name' => 'Peas'],
'fr' => ['name' => 'Pois'],
],
]);

Vegetable::reguard();

self::assertEquals(5, $vegetable->quantity);
self::assertEquals('Peas', $vegetable->translate('en')->name);
self::assertEquals('Pois', $vegetable->translate('fr')->name);

$vegetable = Vegetable::first();
self::assertEquals('Peas', $vegetable->translate('en')->name);
self::assertEquals('Pois', $vegetable->translate('fr')->name);
}

#[Test]
public function it_skips_mass_assignment_if_attributes_non_fillable(): void
{
Expand Down