1 month ago

Can Eloquent accept Unix time for date fields?

Posted 1 month ago by Colin_Laws

I am wondering if I can configure anything on my models or environment to allow setting a date field to an integer, and having Eloquent automatically interpret this as Unix time.

At the moment, it's a bit painful to use Carbon::createFromTimestamp($unixTime) everywhere. I would prefer just being able to set my date fields to Unix time, save, and have Eloquent handle the casting.

To be clear, I want the database type to be Timestamp, and be able to cast Unix time to a timestamp. I am seeing answers on other sites where users want to save Unix time in the database. I know how to achieve that functionality.

Please sign in or create an account to participate in this conversation.