Illuminate query builder, how to convert dates to Carbon object

喜你入骨 提交于 2021-01-29 11:53:37

问题


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:


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.



来源:https://stackoverflow.com/questions/61344180/illuminate-query-builder-how-to-convert-dates-to-carbon-object

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