Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ public function attributesToArray()
return $attributes;
}

/** @inheritdoc */
protected function normalizeCastClassResponse($key, $value)
{
return [$key => $value];
}

/** @inheritdoc */
public function getCasts()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public function testUpdate(): void

$check = User::find($user->_id);
$this->assertEquals(20, $check->age);

$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->update(['skills' => ['Git','Github','Unit testing','Github']]);

$check = User::find($user->_id);
$this->assertCount(3,$check->skills);
$this->assertEquals(['Git','Github','Unit testing'],$check->skills);
}

public function testManualStringId(): void
Expand Down
8 changes: 8 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ protected function username(): Attribute
);
}

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

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