Apr 17, 2023
0
Level 4
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.
Please or to participate in this conversation.