Reputation: 5353
I need to return created_at
and updated_at
attributes in timestamp format but only for my API methods. I don't want to manually loop through results and modify them as I return a lot of relationships so I guess it's not a good way to handle that.
If I set access mutators then everywhere in my app I'll get the dates in timestamps and I'm not sure if that is what I want. Also this is not a good solution since I'll set mutators for each model only because I need to output them in API.
Is there a way to handle that without manually looping through the big response I have and applying Carbon::parse($value)->timestamp;
to all dates?
Upvotes: 2
Views: 1713
Reputation: 214
How about simply overriding Eloquent's method?
/**
* @inheritdoc
*/
protected function serializeDate(\DateTime $date)
{
return $date->getTimestamp();
}
Upvotes: 1