Nested loop issues with course, section, and lesson counting

I’m having trouble with my nested loops for counting course sections and lessons. Here’s my current code:

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

The output is weird. It’s repeating the counts for sections and lessons way too many times. I think there’s an issue with my loops, but I can’t figure out what’s wrong. Can someone help me fix this so it shows the correct counts for each course, section, and lesson?

I’ve encountered similar issues with nested loops in Laravel before. The problem lies in how your loops are structured. You’re iterating over units, but then trying to access topics outside of that loop, which leads to unexpected results.

Here’s a more efficient approach:

<div class='content-wrapper'>
    @foreach($modules as $m)
    <p class='content-wrapper__stats'>
        <span class='content-wrapper__stats-item'>{{ $m->units->count() }} units</span>
        <span class='content-wrapper__stats-item'>{{ $m->units->flatMap->topics->count() }} topics</span>
        <span class='content-wrapper__stats-item'>{{ $m->units->flatMap->topics->sum('length') }} total length</span>
    </p>
    @endforeach
</div>

This restructure eliminates nested loops by using Laravel’s collection methods, which should resolve the counting issues and improve performance. Let me know if you need any clarification on how this works.

hey exploringStars, looks like ur having some loop trouble! i’ve been there too. the problem is ur trying to loop thru topics outside the units loop. try this instead:

@foreach($modules as $m)
<p class="content-wrapper__stats">
    <span>{{ $m->units->count() }} units</span>
    @foreach($m->units as $u)
        <span>{{ $u->topics->count() }} topics</span>
        <span>{{ $u->topics->sum('length') }} length</span>
    @endforeach
</p>
@endforeach

this should fix ur counting issues. lmk if u need more help!

Hey there ExploringStars! :wave:

I can totally see why you’re scratching your head over this one. Nested loops can be tricky beasts, right? :dragon:

I think I might spot the issue here. Your loops aren’t quite nested the way you probably want them to be. The loop for topics is actually outside the units loop, which could explain why you’re seeing some funky repetition.

How about we try restructuring it a bit? Something like this might work better:

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

This way, we’re counting all the units and topics for each module in one go. No nested loops needed!

What do you think? Does this look like it might solve your problem? Give it a shot and let me know how it goes! And hey, if you’re still having trouble, don’t hesitate to ask. We’re all here to learn and help each other out. :blush:

Curious to hear more about your project. What kind of course system are you building?