Skip to content
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

[11.x] Dynamic Model Attribute Mutators #50760

Closed

Conversation

tylernathanreed
Copy link
Contributor

@tylernathanreed tylernathanreed commented Mar 25, 2024

This PR adds the ability to dynamically register model attribute mutators without having to declare them as methods.

See #50759 for a scenario where this would be useful.

Ideally, on the boot method of a model, or an external trait, these mutators could be added:

class User extends Model
{
    public static function boot()
    {
        /** @var array<string,Attribute> */
        $attributes = [
            'example' => Attribute::make(...),
            /* ... some dynamic source ... */
        ];

        foreach ($attributes as $name => $attribute) {
            static::addDynamicAttribute($name, $attribute);
        }
    }
}

// echo $user->example;
// $user->example = 5;

@crynobone
Copy link
Member

Marked as draft since tests are failing.

@crynobone crynobone marked this pull request as draft March 25, 2024 23:03
@driesvints
Copy link
Member

Feel free to resend this one with passing tests, thanks.

@driesvints driesvints closed this Mar 26, 2024
@tylernathanreed
Copy link
Contributor Author

Superseded by #50770

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.

3 participants