Преобразование перед toArray

class Robot extends Model
{
    public function afterFetch()
    {
        if ($this->expiration_at !== null)
            $this->expiration_at = \DateTime::createFromFormat('Y-m-d', $this->expiration_at);
    }

    public function beforeSave()
    {
    if ($this->expiration_at !== null)
            $this->expiration_at = $this->expiration_at->format('Y-m-d');
    }

}
return Robot::findFirst(1)->toArray();
{
expiration_at: {
date: "2019-12-22 11:12:19.000000",
timezone_type: 3,
timezone: "UTC"
}
}

мне нужно преобразовать дату перед функцией -> toArray(), если такое событие ? I need to convert the date before the function -> toArray (), if such an event?

я хочу получить: I want to receive:

{expiration_at:"2019-12-22"}

you have to add an afterSave function

class Robot extends Model
{
    public function afterFetch()
    {
        $this->toDateTime();
    }

    public function afterSave()
    {
        $this->toDateTime();
    }

    // change expiration date from string to DateTime
    private function toDateTime() {
        if ($this->expiration_at !== null)
            $this->expiration_at = \DateTime::createFromFormat('Y-m-d', $this->expiration_at);
    }

    // change DateTime to string just for save data
    public function beforeSave()
    {
    if ($this->expiration_at !== null)
            $this->expiration_at = $this->expiration_at->format('Y-m-d');
    }
}

Good luck

you have to add an afterSave function

then I have to do it like this:

 if (DateTime::createFromFormat('Y-m-d', $coupon->expiration_at) < new DateTime())
 {
}

And I wanted to do it like this

 if ($coupon->expiration_at < new DateTime())
 {
}