Skip to content

Commit ed5425a

Browse files
[10.x] HasCasts returning false instead of true (#46992)
* fix: HasCasts returning false when fully qualified class-name was provided as a second parameter * fix: styling * fix: Removed unecessary PHP check * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent e6585e3 commit ed5425a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,8 @@ protected function getCastType($key)
864864
$convertedCastType = 'immutable_custom_datetime';
865865
} elseif ($this->isDecimalCast($castType)) {
866866
$convertedCastType = 'decimal';
867+
} elseif (class_exists($castType)) {
868+
$convertedCastType = $castType;
867869
} else {
868870
$convertedCastType = trim(strtolower($castType));
869871
}

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ public function testDirtyOnEnumArrayObject()
343343
$this->assertTrue($model->isDirty('asEnumArrayObjectAttribute'));
344344
}
345345

346+
public function testHasCastsOnEnumAttribute()
347+
{
348+
$model = new EloquentModelEnumCastingStub();
349+
$this->assertTrue($model->hasCast('enumAttribute', StringStatus::class));
350+
}
351+
346352
public function testCleanAttributes()
347353
{
348354
$model = new EloquentModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]);
@@ -3047,6 +3053,11 @@ protected function serializeDate(DateTimeInterface $date)
30473053
}
30483054
}
30493055

3056+
class EloquentModelEnumCastingStub extends Model
3057+
{
3058+
protected $casts = ['enumAttribute' => StringStatus::class];
3059+
}
3060+
30503061
class EloquentModelDynamicHiddenStub extends Model
30513062
{
30523063
protected $table = 'stub';

0 commit comments

Comments
 (0)