gpt4 book ai didi

php - Laravel 查询生成器 - 如何求和?

转载 作者:行者123 更新时间:2023-11-29 15:34:03 25 4
gpt4 key购买 nike

我正在记录用户一年中每个月的时间条目。时间采用以下格式:hh:mm:ss

示例:08:32:00

以下是我将月份分组的方式:

  $data = $user->times()->whereYear('start_day', 2019)->get();
$group_months = $data->groupBy(function($entry) {
return Carbon::parse($entry->start_day)->format('m');
});

如何对 $group_months 进行求和,以便获得每个月的总时间值?

这是我的多次迁移

Schema::create('times', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->date('start_day');
$table->text('category');
$table->time('start_time');
$table->time('finish_time');
$table->time('duration');
$table->text('notes');
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');

最佳答案

$monthDurations = $user->times()
->whereYear('start_day', 2019)
->select(
\DB::raw('SUM(TIME_TO_SEC(duration)) as `month_duration_in_seconds`'),
\DB::raw('DATE_FORMAT(start_day, "%Y-%m") as `year_month`'),
'category',
'user_id'
)
->groupBy(\DB::raw('DATE_FORMAT(start_day, "%Y-%m")'), 'category', 'user_id')
->get();

关于php - Laravel 查询生成器 - 如何求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58433283/

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