framework icon indicating copy to clipboard operation
framework copied to clipboard

Eloquent: Concrete Carbon implementation

Open rasmusdencker opened this issue 12 years ago • 4 comments

I just noticed that \Illuminate\Database\Eloquent\Model::asDateTime() is implementing Carbon as a concrete class, which ultimately means that you can't extend Carbon and use that implementation directly with Eloquent. In my case, I wanted to be able to localize diffForHumans(). I extended Carbon, re-wrote the method and bound the new implementation to Carbon, but Eloquent doesn't react to that unfortunately.

I could just change the import and alias it as Carbon at the top of Model.php - or I could write the changes directly to Carbon. However, I feel like a bad boy changing anything in /vendor obviously. So at the time being I'm just instantiating a my own Carbon-extension (CarbOnRoids, wahaha) and passing created_at and updated_at into that, but in the long run that's also a messy operation.

How about making use of the service container when resolving the Carbon instance in Eloquent?

Best Regards, Rasmus Dencker

rasmusdencker avatar Oct 10 '14 08:10 rasmusdencker

+1 on this one. Don't feel like extending the Model to overwrite this is the way to go.

jayvdg avatar Oct 11 '14 15:10 jayvdg

Yep - that's another option; extending both Model and Carbon, but that also seems like bit of a workaround for such a simple task.

rasmusdencker avatar Oct 13 '14 07:10 rasmusdencker

:+1:

mikehayesuk avatar Oct 14 '14 09:10 mikehayesuk

You shouldn't be extending the carbon object. If you want localisation, I suggest using a model presenter.

GrahamCampbell avatar Dec 28 '14 14:12 GrahamCampbell