php—在laravel 5.5中对字符串调用成员函数format()

km0tfn4u  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(277)

我正在尝试获取特定用户的总出勤时间。在这个方法中,我得到的是总结果,但现在的问题是设置持续时间的格式。我试着这样做,但得到的错误如上所述。有人能帮帮我吗-这是方法-

public function index(Request $request)
{
  $id = Auth::id();
  $total_duration = DB::table('attendances')
                    ->select('duration')
                    ->where('teacher_id', '=', $id)
                    ->sum('duration');
  return view('teachers.attendance.index', compact('teacher', 'attendances', 'att_id', 'total_duration'));
 }

在我的index.blade.php中-

<tr>
  <td colspan="4">Total</td>
  <td>
       {{ $total_duration->format('H:i:s') }}
  </td>
</tr>

f5emj3cl

f5emj3cl1#

$total\u duration返回一个字符串,因此不能对其调用函数。如果要更改日期字符串的格式。你可以试试。
更新:完整代码为:

$total_duration = DB::table('attendances')->selectRaw('SEC_TO_TIME( SUM( 
TIME_TO_SEC( `duration` ) ) ) as total')->first();

而$total_duration->total将是您需要的值

相关问题