Skip to content

Commit f6aa120

Browse files
authored
Merge pull request spatie#48 from d1am0nd/feature/toArray_conditionally_adds_key
[spatie#47] toArray on HasBinaryUuid conditionally adds model key
2 parents adf51bf + a9c2808 commit f6aa120

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/HasBinaryUuid.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ public function toArray()
7777
return parent::toArray();
7878
}
7979

80-
return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]);
80+
$data = parent::toArray();
81+
82+
if (isset($data[$this->getKeyName()])) {
83+
$data[$this->getKeyName()] = $this->uuid_text;
84+
}
85+
86+
return $data;
8187
}
8288

8389
public function getUuidTextAttribute(): ?string

tests/Feature/HasBinaryUuidTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,14 @@ public function it_prevents_decoding_the_uuid_when_the_model_does_not_exist()
164164
$this->assertEmpty($model->toArray());
165165
$this->assertNull($model->uuid_text);
166166
}
167+
168+
/** @test */
169+
public function it_prevents_decoding_model_key_when_it_is_not_included_in_attributes()
170+
{
171+
$model = TestModel::create();
172+
$model->setRawAttributes(['test' => 'test']);
173+
$array = $model->toArray();
174+
175+
$this->assertFalse(isset($array[$model->getKeyName()]));
176+
}
167177
}

0 commit comments

Comments
 (0)