不显示 laravel blade 中的数组值 - 显示为未定义的索引

Not Displaying Array values in laravel blade - Showing as undefined index

我有一个数组列表,数组包含一些数据,我需要显示在laravel blade 文件中。

 @foreach ($taskDetail as $taskRow)
                    <tr>
                        <th>{{ $taskRow['taskName'] }}</th>
                        <th>Total : {{ $taskRow['takenHours'] }} </th>
                    </tr>
                          @if (count($taskRow['php'])>0)    
                            <tr>
                                <th>PHP Team</th>
                                <th>Hours</th>
                            </tr>
                            @foreach ($taskRow['php'] as $row)

                                <tr>
                                    <td> {{ $row['firstName'] }}</td>
                                    <td> {{ $row['taskTakenHours'] }}</td>
                                </tr>
                            @endforeach  
                                <tr>
                                    <th>Total</th>
                                    <th>{{ $row[count($taskRow['php'])-1]['totalHours'] }}</th>
                                </tr>
                            @endif

                @endforeach

在第二个 foreach 语句中,出现以下错误。

Undefined index: firstName (View: C:\Azure Files\PM Tools\BPMT_API\resources\views\report-teamwise.blade.php

转储 dd($row) 的值时如下所示

array:12 [
  "eachTasktakenHr" => "24:56:00"
  "id" => 61
  "userId" => 5
  "taskTakenHours" => "05:00"
  "hours" => "5"
  "minutes" => "0"
  "dateOfEntry" => "2019-12-24"
  "taskStartDate" => "24.12.2019"
  "firstName" => "Deepak"
  "lastName" => "Kotian"
  "taskName" => "Meeting /Discussion"
  "deptName" => "PHP"
]

您可能有一行 firstName 为空。试试这个:

空合并运算符


{{ $row['firstName'] ?? '' }}

三元

{{ isset($row['firstName']) ? $row['firstName'] : '' }}

如果未设置 $row['firstName'],它将被设置为空字符串。