Skip to content

Mutator can not handle array #2635

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

Closed
wants to merge 7 commits into from
Closed
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
11 changes: 11 additions & 0 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use function abs;
use function array_key_exists;
use function array_key_first;
use function array_keys;
use function array_unique;
use function array_values;
Expand Down Expand Up @@ -251,6 +252,16 @@ public function attributesToArray()
return $attributes;
}

/** @inheritdoc */
protected function normalizeCastClassResponse($key, $value)
{
if (! is_array($value) || (count($value) && is_numeric(array_key_first($value)))) {
return [$key => $value];
}

return $value;
}

/** @inheritdoc */
public function getCasts()
{
Expand Down
14 changes: 13 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,20 @@ public function testUpdate(): void
$raw = $user->getAttributes();
$this->assertInstanceOf(ObjectID::class, $raw['_id']);

$check->skills = ['PHP', 'Laravel', 'MongoDB', 'Laravel'];
$check->save();

$check = User::find($user->_id);
$this->assertCount(3, $check->skills);
$this->assertEquals(['PHP', 'Laravel', 'MongoDB'], $check->skills);

$check->fullname = ['Hans', 'Thomas'];
$check->save();

$check = User::find($user->_id);
$this->assertEquals(20, $check->age);
self::assertEquals('Hans', $check->first_name);
self::assertEquals('Thomas', $check->last_name);
self::assertEquals('Hans Thomas', $check->fullname);
}

public function testManualStringId(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use MongoDB\Laravel\Eloquent\MassPrunable;
use MongoDB\Laravel\Eloquent\Model as Eloquent;

use function array_unique;

/**
* @property string $_id
* @property string $name
Expand Down Expand Up @@ -109,6 +111,22 @@ protected function username(): Attribute
);
}

protected function skills(): Attribute
{
return Attribute::make(
get: fn ($value) => $value,
set: fn ($values) => array_unique($values)
);
}

protected function fullname(): Attribute
{
return Attribute::make(
get: fn ($value) => $this->first_name . ' ' . $this->last_name,
set: fn ($values) => ['first_name' => $values[0], 'last_name' => $values[1]]
);
}

public function prunable(): Builder
{
return $this->where('age', '>', 18);
Expand Down