So I'm trying to display a "Group By" report in a HTML table.
The user selects group by conditions from a set of select fields, between 1 - 3.
For example:
Group by 1: Domain
Then Group By 2: Storefront
Then Group By 3: Browser
<table>
<thead>
...
</thead>
<tbody>
<tr>
<th scope="row" colspan="12" class="alert-info pl-1">
Domain1.com
</th>
</tr>
<tr>
<th scope="row" colspan="12" class="alert-info pl-2">
v1
</th>
</tr>
<tr>
<th scope="row" class="pl-3">↪ chrome</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row" class="pl-3">↪ internet explorer</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row" colspan="12" class="alert-info pl-1">
Domain2.com
</th>
</tr>
<tr>
<th scope="row" colspan="12" class="alert-info pl-2">
v2
</th>
</tr>
...
...
...
</tbody>
</table>
This here is the Collection I have to display the table:
Collection {#1329 ▼
#items: array:6 [▼
"domain1.com" => Collection {#1144 ▼
#items: array:4 [▼
"v1" => Collection {#1122 ▼
#items: array:5 [▼
"chrome" => Collection {#1117 ▼
#items: array:1 [▼
0 => {#927 ▼
+"product_group": "group_1"
+"views": "17"
+"visits": "5"
+"clicks": "19"
+"conversions": "14"
+"revenue": "12.51"
+"domain_name": "domain1.com"
+"store_version": "v1"
+"browser": "chrome"
}
]
}
"firefox" => Collection {#1118 ▶}
"internet explorer" => Collection {#1119 ▶}
"microsoft edge" => Collection {#1120 ▶}
"safari" => Collection {#1121 ▶}
]
}
"v2" => Collection {#1129 ▶}
"v3" => Collection {#1136 ▶}
"v4" => Collection {#1143 ▶}
]
}
"domain2.com" => Collection {#1181 ▶}
"domain3.com" => Collection {#1217 ▶}
"domain4.com" => Collection {#1254 ▶}
"domain5.com" => Collection {#1291 ▶}
"domain6.com" => Collection {#1328 ▶}
]
}
Recursion in Blade:
@foreach ($rows as $key => $row)
@include('partials.rows', [
'key' => $key,
'row' => $row
])
@endforeach
Then the partial template:
@if($row instanceof \Illuminate\Support\Collection)
@foreach($row as $key => $child)
@include('partials.rows', [
'key' => $key,
'row' => $child
])
@if(! $loop->last)
<tr>
<th scope="row" colspan="12" class="alert-info pl-{{ $loop->depth }}">
{{ $key }}
</th>
</tr>
@else
<tr>
<th scope="row" class="pl-{{ $loop->depth }}">
↪
{{ $key }}
</th>
<td>{{ $child->views }}</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
@endif
@endforeach
@endif
The problem comes now where I am trying to display the metrics in the final row of each array inside the last collection.
There is an error like:
"Property [views] does not exist on this collection instance."
But it clearly does exist, when I do a die & dump in the above table structure in the @else I get this result:
@else
{{ dd($child) }}
<tr>
gives:
{#927 ▼
+"product_group": "group_1"
+"views": "17"
+"visits": "5"
+"clicks": "19"
+"conversions": "14"
+"revenue": "12.51"
+"domain_name": "domain1.com"
+"store_version": "v1"
+"browser": "chrome"
}
So, my head hurts :)
Do you guys have a better way how to achieve what I want?
Or see where I am going wrong in the above?
Thank you!