Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robwinkky's avatar

New Mutator/accessor refactor

I have this old school mutator/accessor and I want to refactor it to the new school method.

public function getDateReceivedAttribute($value)
    {
        return $value ? Carbon::parse($value)->format('Y-m-d') : null;
    }

public function setDateReceivedAttribute($value)
    {
        $this->attributes['date_received'] = $value ? Carbon::createFromFormat('Y-m-d', $value)->format('Y-m-d') : null;
    }

I tried

     protected function dateReceived(): Attribute
     {
         return Attribute::make(
             get: fn (string $value) => Carbon::parse($value)->format('Y-m-d'),
             set: fn (string $value) => Carbon::createFromFormat('Y-m-d', $value)->format('Y-m-d'),
         );
    }

and it just doesn't work. I was hoping copilot would help me but alas it didn't! The old style works just fine, but I am not smart enough to know what question to research to figure out what I am doing wrong.

0 likes
0 replies

Please or to participate in this conversation.