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

Php 在laravel 5.5中对字符串调用成员函数format(),php,mysql,datetime-format,laravel-eloquent,Php,Mysql,Datetime Format,Laravel Eloquent,我正在尝试获取特定用户的总出席时间。在这个方法中,我得到的是总的结果,但现在的问题是设置持续时间的格式。我试着这样做,但得到的错误如上所述。有人能帮我吗- 以下是方法- public function index(Request $request) { $id = Auth::id(); $total_duration = DB::table('attendances') ->select('duration')

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

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>

全部的
{{$total_duration->format('H:i:s')}

您的$total\u duration返回一个字符串,因此无法对其调用函数。如果要更改日期字符串的格式。你可以试试

更新:完整代码为:

$total_duration = DB::table('attendances')->selectRaw('SEC_TO_TIME( SUM( TIME_TO_SEC( `duration` ) ) ) as total')->first(); $total_duration=DB::table('attentations')->selectRaw('SEC_TO_TIME)(总和( 总时长为秒(`duration`)')->first();
$total_duration->total将是您需要的值

,这是因为
total_duration
是一个字符串,而不是一个对象。您不使用时间戳吗?因此,当use selectedI遵循以下步骤时,应该计算持续时间字段,但问题尚未解决@AllloushNow获取此错误-DateTime::\uu construct():未能解析位置4(4)处的时间字符串(14924):意外字符未正确计算总持续时间您能更清楚地告诉我吗?现在的结果是什么?您期望的结果是什么?每个表列的“duration”值我期望在字段total(在view total字段中)中获得总持续时间-01:49:24。但我会根据您的任务得到00:00:70的结果。如何在dtb中存储持续时间值?你能告诉我你数据库的结构吗。