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