问题
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