Search code examples
laravellaravel-5php-carbon

Laravel Query Builder: select TIMESTAMP as Carbon object


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

Solution

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

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