Search code examples
phplaravelpdflaravel-bladedompdf

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


I have list of an array, array containes some data, I need to display in the laravel blade file.

 @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

In the second foreach statement I am getting the below error.

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

On dumping the values of dd($row) displayes like this below

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

Solution

  • You may have a single row where firstName is null. Try this:

    Null Coalesce Operator

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

    or

    Ternary

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

    If the $row['firstName'] is not set it will be set to an empty string.