如何在 Laravel 视图中格式化 Carbon 日期时间值

问题描述 投票:0回答:1

我在变量 $audits 下有这个嵌套数组,我将其移交给 Laravel 5.8 视图:

array:4 [▼
  0 => array:3 [▼
    "action" => "Case initiated by:"
    "user" => "Doe, Jane"
    "timestamp" => Carbon @1558353758 {#419 ▶}
  ]
  1 => array:3 [▼
    "action" => "New head mediator:"
    "user" => "Doe, Jane"
    "timestamp" => Carbon @1558353758 {#430 ▶}
  ]
  2 => array:3 [▼
    "action" => "Case closed by:"
    "user" => "Doe, Jane"
    "timestamp" => Carbon @1558353787 {#467 ▶}
  ]
  3 => array:3 [▼
    "action" => "Case re-opened by:"
    "user" => "Doe, Jane"
    "timestamp" => Carbon @1558353791 {#474 ▶}
  ]
]

我的目标是填充视图中的表格以获得以下输出:

Action                  User        Date/time (UTC)
-------------------------------------------------------
Case initiated by:      Doe, Jane   2019-05-20 12:02:38
New head mediator:      Doe, Jane   2019-05-20 12:02:38
Case closed by:         Doe, Jane   2019-05-20 12:03:07
Case re-opened by:      Doe, Jane   2019-05-20 12:03:11

为了实现这一点,我希望能够像这样循环:

<table>
  <th>Action</th>
  <th>User</th>
  <th>Date/time (UTC)</th>

 @foreach ($audits as $audit)
    @foreach ($audit as $value)
      <tr>
        <td>{{ $value['action'] }}</td>
        <td>{{ $value['user'] }}</td>
        <td>{{ $value['timestamp'] }}</td>
      </tr>
    @endforeach
  @endforeach
</table>

但是,这会给我错误消息,因为视图似乎无法从数组中检索此类特定值,因为它只能处理字符串。 所以,我尝试将数组转换为对象,使用JSON编码等,但没有成功。

在控制器中构造数组的方式是这样的(也许有人可以指导我如何创建正确的集合或对象,以便视图可以更好地处理它):

public function show(Project $project)
  {

    $audits_raw= $project->audits;

    foreach ($audits_raw as $audit_raw) {

      foreach ($audit_raw->new_values as $action => $id) {

        if ($action == 'initiator_id') {
          $actions[] = 'Case initiated by:';
          $users[] = $this->userName($id);
          $timestamps[] = $audit_raw->updated_at;
        } elseif ($action == 'head_mediator_id') {
          $actions[] = 'New head mediator:';
          $users[] = $this->userName($id);
          $timestamps[] = $audit_raw->updated_at;
        } elseif ($action == 'marked_for_deletion' && $id == 1) {
          $actions[] = 'Case closed by:';
          $users[] = $this->userName($audit_raw->user_id);
          $timestamps[] = $audit_raw->updated_at;
        } elseif ($action == 'marked_for_deletion' && $id == 0) {
          $actions[] = 'Case re-opened by:';
          $users[] = $this->userName($audit_raw->user_id);
          $timestamps[] = $audit_raw->updated_at;
        }
      }
    }

    $audits = array_map(function ($action, $user, $timestamp) {
      return array_combine(
        ['action', 'user', 'timestamp'],
         [$action, $user, $timestamp]
      );
     }, $actions, $users, $timestamps);

    return view('admin.billing.show', compact('project', 'audits', 'actions'));

  }

  protected function userName($id)
  {
    $user = User::where('id', $id)->first();
    $username = $user->last_name.', '.$user->first_name;
    return $username;
  }
php laravel laravel-5 model-view-controller php-carbon
1个回答
0
投票

更改视图。

  @foreach($audits as $key)
  <tr>
    <td>{{ $key['action'] }}</td>
    <td>{{ $key['user'] }}</td>
    <td>{{ Carbon\Carbon::parse($key['timestamp'])->format('Y-m-d H:i:s') }} . 
    </td>
  </tr>
 @endforeach
© www.soinside.com 2019 - 2024. All rights reserved.