Skip to content

[9.x] Adds Attribute::with - #41014

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
bvtterfly:attribute-with-constructor
Feb 15, 2022
Merged

[9.x] Adds Attribute::with#41014
taylorotwell merged 4 commits into
laravel:9.xfrom
bvtterfly:attribute-with-constructor

Conversation

@bvtterfly

Copy link
Copy Markdown
Contributor
  • This PR adds the possibility to create Attribute using static with method to Laravel 9.

Why?

Laravel has a very expressive syntax, but I think the current syntax of Attributes isn't like other parts of laravel. So I made this PR with a cleaner and more readable way to create them, significantly when disabling object caching.

Current approach:


    public function uppercase(): Attribute
    {
        return (new Attribute(
            get: fn ($value) => strtoupper($value),
            set: fn ($value) => strtoupper($value)
        ))->withoutObjectCaching();
    }


New approach:


    public function uppercase(): Attribute
    {
        return Attribute::with(
            get: fn ($value) => strtoupper($value),
            set: fn ($value) => strtoupper($value)
        )->withoutObjectCaching();
    }


@edemots

edemots commented Feb 15, 2022

Copy link
Copy Markdown
Contributor

Hey! Since it mimics other parts of Laravel behavior for instantiation, why not use make as the method name?

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