Laravel Query Builder: select TIMESTAMP as Carbon object

前提是你 提交于 2020-12-12 01:58:08

问题


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();

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);

回答1:


If you don't use Eloquent, then you should create a Carbon object manually:

$carbonDate = Carbon::parse($data['DateCreated'])


来源:https://stackoverflow.com/questions/42810096/laravel-query-builder-select-timestamp-as-carbon-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!