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
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function attributesToArray()
// as these attributes are not really in the attributes array, but are run
// when we need to array or JSON the model for convenience to the coder.
foreach ($this->getArrayableAppends() as $key) {
$attributes[$key] = $this->mutateAttributeForArray($key, null);
$attributes[$key] = $this->mutateAttributeForArray($key, $this->getAttributeFromArray($key));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$attributes[$key] = $this->mutateAttributeForArray($key, $this->getAttributeFromArray($key));
$attributes[$key] = $this->mutateAttributeForArray(
$key, $attributes[$key] ?? $this->getAttributeFromArray($key)
);

@lazerg lazerg Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this, but I don't think this is safe. mutateAttributeForArray always expects the raw, pre-mutation value, that's exactly how addMutatedAttributesToArray calls it a few lines earlier for the normal attributes.

If the append key also has an accessor mutator, addMutatedAttributesToArray already ran the accessor on it and stored the mutated output in $attributes[$key] before we get to the appends loop. Using $attributes[$key] here would feed that already-mutated value back into mutateAttributeForArray, so the accessor runs twice: once on the raw value, once on its own output.

getAttributeFromArray gives the raw stored value, the same thing a normal (non-appended) access to that attribute passes into its mutator. That's what keeps accessors getting what they expect.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes complete sense! Excellent point regarding avoiding double-mutation when addMutatedAttributesToArray has already processed the attribute into $attributes[$key].

Passing the raw value via $this->getAttributeFromArray($key) indeed guarantees that the accessor receives the pristine stored value, exactly matching direct property access.

Appreciate the detailed explanation! Looks solid.

}

return $attributes;
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,15 @@ public function testAppendingOfAttributes()
$this->assertSame([], $model->toArray());
}

public function testToArrayPassesRealValueToAppendedAccessor()
{
$model = new EloquentModelAppendsWithExistingAttributeStub;
$model->price = 100;

$this->assertSame(100, $model->price);
$this->assertSame(100, $model->toArray()['price']);
}

public function testMergeAppendsMergesAppends()
{
$model = new EloquentModelAppendsStub;
Expand Down Expand Up @@ -4307,6 +4316,18 @@ public function getStudlyCasedAttribute()
}
}

class EloquentModelAppendsWithExistingAttributeStub extends Model
{
protected $guarded = [];

protected $appends = ['price'];

protected function price(): Attribute
{
return Attribute::get(fn ($value) => $value);
}
}

class EloquentModelGetMutatorsStub extends Model
{
public static function resetMutatorCache()
Expand Down