Illuminate query builder, how to convert dates to Carbon object

363 Views Asked by At

Is there any chance to use Illuminate Query builder to get dates as Carbon objects instead of strings?

For example:

$user=DB::table('users')->select(["id","lastLogin"])->where("id",1)->first();

$user->lastLogin; // <--- Carbon instead of string!
1

There are 1 best solutions below

6
UfguFugullu On

You can use Carbon::parse($user->lastLogin), but I think there is no native way to get dates as carbon objects without using eloquent. An example is this answer on stackoverflow.

I think working with eloquent models will make the work much easier. So you can set the $dates property to get the lastLogin as carbon object. More informations at laravel docs.