Skip to content

Conversation

@nickakitch
Copy link
Contributor

When using the new attribute casting method from PR #40022 you are currently unable to include the method in the 'appends' property as you would when using the older method.

Currently:

$appends = ['title'];

public function title(): Attribute
{
    return ucfirst($this->name);
}

$model->toArray(); // throws a BadMethodCallException

This PR allows methods that return the Attribute object to be included in the 'appends' property

@GrahamCampbell GrahamCampbell changed the title Fixed the appends property to work with the new Accessor method [8.x] Fixed the appends property to work with the new Accessor method Jan 22, 2022
@taylorotwell
Copy link
Member

Works fine for me - your attribute is not defined correctly?

public function title(): Attribute
{
    return new Attribute(
        fn ($value, $attributes) => ucfirst($attributes['name']),
    );
}

@nickakitch
Copy link
Contributor Author

From the looks of things the case matters:

$appends = ['firstName']; // won't work
$appends = ['first_name']; // works

With the old method camel case also worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants