orderBy(-6ren">
gpt4 book ai didi

Laravel Eloquent Pluck 生成了错误的数据

转载 作者:行者123 更新时间:2023-12-02 19:12:04 25 4
gpt4 key购买 nike

尝试使用 pluck 获取创建日期。

        $campaigns_dates = CampaignHistory::where('status', "Complete")->orderBy( 'created_at', 'ASC' )->pluck('created_at');
$campaigns_dates = json_decode( $campaigns_dates );
return $campaigns_dates;

//得到这个

["2020-06-29T14:19:03.000000Z","2020-06-29T14:19:42.000000Z","2020-07-29T14:23:54.000000Z","2020-08-28T14:55:53.000000Z","2020-08-29T14:17:40.000000Z","2020-09-29T14:18:27.000000Z","2020-09-29T14:21:13.000000Z"]

//但是想要这个

Example: ["2020-06-29 14:19:03","2020-06-29 14:19:42"]

最佳答案

您可以使用 map 操作将日期重新格式化为给定格式:

public function index() {

$campaigns_dates = CampaignHistory::orderBy('created_at', 'ASC')
->pluck('created_at')
->map
->format('Y-m-d H:i:s');

$campaigns_dates = json_decode($campaigns_dates);
return $campaigns_dates;
}

替代解决方案:有一个使用 selectRaw() 方法的替代解决方案,感谢 @OMR提供此解决方案:

public function index() {

$campaigns_dates = CampaignHistory::orderBy('created_at', 'ASC')
->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as formatted_date")
->pluck('formatted_date');

$campaigns_dates = json_decode($campaigns_dates);
return $campaigns_dates;
}

关于Laravel Eloquent Pluck 生成了错误的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64140574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com