Skip to content

Commit 49d77f1

Browse files
committed
Fix Enums not being cast when calling Model::toArray()
1 parent 7669dc2 commit 49d77f1

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/Eloquent/Model.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5-
use function array_key_exists;
65
use DateTimeInterface;
7-
use function explode;
86
use Illuminate\Contracts\Queue\QueueableCollection;
97
use Illuminate\Contracts\Queue\QueueableEntity;
108
use Illuminate\Contracts\Support\Arrayable;
@@ -13,11 +11,14 @@
1311
use Illuminate\Support\Arr;
1412
use Illuminate\Support\Facades\Date;
1513
use Illuminate\Support\Str;
16-
use function in_array;
1714
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1815
use MongoDB\BSON\Binary;
1916
use MongoDB\BSON\ObjectID;
2017
use MongoDB\BSON\UTCDateTime;
18+
19+
use function array_key_exists;
20+
use function explode;
21+
use function in_array;
2122
use function uniqid;
2223

2324
abstract class Model extends BaseModel
@@ -556,7 +557,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
556557
}
557558

558559
if ($this->isEnumCastable($key) && (! $castValue instanceof Arrayable)) {
559-
$castValue = $castValue !== null ? $this->getStorableEnumValue($attributes[$key]) : null;
560+
$castValue = $castValue !== null ? $this->getStorableEnumValue($castValue) : null;
560561
}
561562

562563
if ($castValue instanceof Arrayable) {

tests/ModelTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,19 @@ public function testFirstOrCreate(): void
786786
$check = User::where('name', $name)->first();
787787
$this->assertEquals($user->_id, $check->_id);
788788
}
789+
790+
public function testEnumCast(): void
791+
{
792+
$name = 'John Member';
793+
794+
$user = new User();
795+
$user->name = $name;
796+
$user->member_status = MemberStatus::Member;
797+
$user->save();
798+
799+
/** @var User $check */
800+
$check = User::where('name', $name)->first();
801+
$this->assertSame(MemberStatus::Member->value, $check->getRawOriginal('member_status'));
802+
$this->assertSame(MemberStatus::Member, $check->member_status);
803+
}
789804
}

tests/models/MemberStatus.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
enum MemberStatus: string
4+
{
5+
case Member = 'MEMBER';
6+
}

tests/models/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @property \Carbon\Carbon $created_at
2525
* @property \Carbon\Carbon $updated_at
2626
* @property string $username
27+
* @property MemberStatus member_status
2728
*/
2829
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
2930
{
@@ -36,6 +37,7 @@ class User extends Eloquent implements AuthenticatableContract, CanResetPassword
3637
protected $casts = [
3738
'birthday' => 'datetime',
3839
'entry.date' => 'datetime',
40+
'member_status' => MemberStatus::class,
3941
];
4042
protected static $unguarded = true;
4143

0 commit comments

Comments
 (0)