Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,9 @@ public function discardChanges()
{
[$this->attributes, $this->changes, $this->previous] = [$this->original, [], []];

$this->classCastCache = [];
$this->attributeCastCache = [];

return $this;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,21 @@ public function testDiscardChanges()
$this->assertNull($user->getAttribute('name'));
}

public function testDiscardChangesWithCasts()
{
$model = new EloquentModelWithPrimitiveCasts();

$model->address_line_one = '123 Main Street';

$this->assertEquals('123 Main Street', $model->address->lineOne);
$this->assertEquals('123 MAIN STREET', $model->address_in_caps);

$model->discardChanges();

$this->assertNull($model->address->lineOne);
$this->assertNull($model->address_in_caps);
}

public function testHasAttribute()
{
$user = new EloquentModelStub([
Expand Down Expand Up @@ -3994,6 +4009,17 @@ public function thisIsAlsoFine(): Attribute
{
return Attribute::get(fn () => 'ok');
}

public function addressInCaps(): Attribute
{
return Attribute::get(
function () {
$value = $this->getAttributes()['address_line_one'] ?? null;

return is_string($value) ? strtoupper($value) : $value;
}
)->shouldCache();
}
}

enum CastableBackedEnum: string
Expand All @@ -4003,6 +4029,12 @@ enum CastableBackedEnum: string

class Address implements Castable
{
public function __construct(
public ?string $lineOne = null,
public ?string $lineTwo = null
) {
}

public static function castUsing(array $arguments): CastsAttributes
{
return new class implements CastsAttributes
Expand Down