Trouble with nested loops for course, section, and lesson counts

Hey guys, I’m stuck on a problem with my Laravel blade template. I’m trying to display counts for course sections and lessons, but my output is all messed up. Here’s what I’ve got:

<div class="info-box">
    @foreach($modules as $m)
    <p class="info-box__stats">
    @foreach($m->units as $u)
        <span class="info-box__stats-item">
            {{ $u->count('id') }} unit
        </span>
        @endforeach
        @foreach($u->topics as $t)
        <span class="info-box__stats-item">
            {{ $t->count('id') }} topic
        </span>
        <span class="info-box__stats-item">
            {{ $t->count('length') }} topic length
        </span>
        @endforeach
    </p>
    @endforeach
</div>

Instead of getting the right counts, I’m seeing repeated numbers like:

30 unit 30 unit 30 unit … 15 topic 15 topic length 15 topic 15 topic length …

This repeats for all my courses. Any ideas what I’m doing wrong with these loops? Thanks!

yo dolphin, ive seen this before. ur loops r nested wrong. try this:

@foreach($modules as $m)

{{ $m->units->count() }} units


{{ $m->units->flatMap->topics->count() }} topics


{{ $m->units->flatMap->topics->sum(‘length’) }} total length


@endforeach

this counts everything at the module lvl. should fix ur issue bro. lmk if it works!

Hey SwimmingDolphin! :dolphin:

Looks like you’re caught in a bit of a loop tangle there! I’ve been in similar situations before, and it can be super frustrating. Have you considered restructuring your loops a bit?

What if you tried something like this:

<div class="info-box">
    @foreach($modules as $m)
    <p class="info-box__stats">
        <span class="info-box__stats-item">
            {{ $m->units->count() }} units
        </span>
        <span class="info-box__stats-item">
            {{ $m->units->flatMap->topics->count() }} topics
        </span>
        <span class="info-box__stats-item">
            {{ $m->units->flatMap->topics->sum('length') }} total topic length
        </span>
    </p>
    @endforeach
</div>

This way, you’re counting at the module level instead of looping through each unit and topic. It might solve your repeating numbers issue.

Have you tried using Laravel’s query builder to get these counts? Sometimes it can be more efficient than doing it in the view.

What do you think? Does this help at all or am I barking up the wrong tree? Let me know if you need any clarification!

I’ve encountered similar issues with nested loops in Laravel before. The problem likely stems from how you’re structuring your loops and accessing data.

Instead of nesting loops for units and topics, try aggregating the data at the module level:

<div class=\"info-box\">
    @foreach($modules as $m)
    <p class=\"info-box__stats\">
        <span class=\"info-box__stats-item\">
            {{ $m->units()->count() }} units
        </span>
        <span class=\"info-box__stats-item\">
            {{ $m->units()->withCount('topics')->get()->sum('topics_count') }} topics
        </span>
        <span class=\"info-box__stats-item\">
            {{ $m->units()->with('topics')->get()->flatMap->topics->sum('length') }} total length
        </span>
    </p>
    @endforeach
</div>

This approach should resolve the repeated counts issue by calculating totals for each module. It also improves performance by reducing database queries. Let me know if you need further clarification on this solution.