phplaravellaravel-5model-view-controllerphp-carbon

How to format a Carbon datetime value in a Laravel view


I have this nested array under the variable $audits which I hand over to a Laravel 5.8 view:

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 ▶}
  ]
]

My goal is to fill a table in the view to get the following output:

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

To achieve that, I had hoped to be able to loop through like this:

<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>

However, this will give me error messages since the view doesn't seem to be able to retrieve such specific values from arrays as it can only handle strings. So, I tried to transform the array into objects, use JSON encoding etc. but to no avail.

The way the array gets constructed in the controller is like this (maybe somebody can point me in a direction how to make a proper collection or object so that it can be handled better by the view):

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;
  }

Solution

  • Change the view.

      @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