Skip to content

[8.x] Fixed the appends property to work with the new Accessor method #40551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

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