I have a Message model like this :
class Message extends Model
{
protected $primaryKey = 'message_id';
protected $dates = ['created_at', 'updated_at', 'receiver_deleted_at', 'sender_deleted_at', 'sender_read_at', 'receiver_read_at'];
}
As you can see there are some date fields that hold dates as DATETIME(YYYY-MM-DD HH:MM:SS) format.
But I want when get an instance of model all date fields convert to unix-timestamp format and then return.
I know that can use accessor like this for every field :
public function getCreatedAtAttribute()
{
dd(strtotime($this->attributes['created_at']));
return strtotime($this->attributes['created_at']);
}
But in this case I must define a function like above for each field.
But I want an easier way that force model to return date fields as unix-timestamp format.
$this->created_at->format('U');should work.