Eloquent: Concrete Carbon implementation
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
+1 on this one. Don't feel like extending the Model to overwrite this is the way to go.
Yep - that's another option; extending both Model and Carbon, but that also seems like bit of a workaround for such a simple task.
:+1:
You shouldn't be extending the carbon object. If you want localisation, I suggest using a model presenter.