【问题标题】:Call to a member function format() on string in laravel 5.5在 laravel 5.5 中调用字符串上的成员函数 format()
【发布时间】:2018-01-05 09:36:54
【问题描述】:

我正在尝试获取特定用户的总出席时间。在该方法中,我得到了总结果,但是,现在的问题是格式化持续时间。我正在尝试这样,但出现上述错误。有人可以帮我吗- 这是方法 -

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>

【问题讨论】:

标签: php mysql datetime-format laravel-eloquent


【解决方案1】:

你的 $total_duration 返回一个字符串,所以你不能在它上面调用函数。如果您想更改日期字符串的格式。你可以试试。

更新:完整代码是:

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

并且 $total_duraion->total 将是您需要的值

【讨论】:

  • 现在出现此错误 - DateTime::__construct(): 无法在位置 4 (4) 解析时间字符串 (14924):意外字符
  • 没有正确计算总时长
  • 你能告诉我更清楚吗?现在的结果是什么,你期望的结果是什么
  • 根据表格列的“持续时间”值,我希望在总计字段中获得总持续时间 - 01:49:24(在视图总计字段中)。但是根据您的任务,我会在 00:00:70 得到结果。
  • 如何在 dtb 中存储持续时间值?你能告诉我你的数据库的结构吗?
猜你喜欢
  • 2018-10-05
  • 2017-03-24
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多