本文介绍了Laravel查询生成器:选择TIMESTAMP作为Carbon对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用查询生成器将时间戳列选择为Carbon日期对象?以下代码段中的 DateCreated
:
Is it possible to select a timestamp column as a Carbon date object using the query builder? DateCreated
in the snippet below:
$entities = DB::table('translation_review as tr')
...
->select('tr.AuthorID', 't.LanguageID', 't.DateCreated' /* <--- this one! */)
->get();
更新1 :
我已经通过遍历结果集并手动更改 DateChanged 属性来手动解决了此问题,但是我对这种解决方案不满意.感觉很优雅.
Update 1:
I have solved this manually by iterating over the result set and changing the DateChanged property manually, but I am not satisfied with this solution. It feels inelegant.
foreach ($entities as $entity)
$entity->DateCreated = Carbon::createFromFormat('Y-m-d H:i:s', $entity->DateCreated);
推荐答案
如果您不使用Eloquent,则应手动创建Carbon对象:
If you don't use Eloquent, then you should create a Carbon object manually:
$carbonDate = Carbon::parse($data['DateCreated'])
这篇关于Laravel查询生成器:选择TIMESTAMP作为Carbon对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!