Trouble with nested loops for counting course components

I’m having issues with my nested foreach loops. I’m trying to count sections, lessons, and lesson durations for each course. But the output is really weird. Here’s my current code:

<div class="content-wrapper">
    @foreach($subjects as $subject)
    <p class="content-summary">
    @foreach($subject->units as $unit)
        <span class="content-detail">
            {{ $unit->count('id') }} unit
        </span>
        @endforeach
        @foreach($unit->topics as $topic)
        <span class="content-detail">
            {{ $topic->count('id') }} topic
        </span>
        <span class="content-duration">
            {{ $topic->count('length') }} topic length
        </span>
        @endforeach
    </p>
    @endforeach
</div>

The output repeats numbers like crazy. It shows 30 units over and over, then 15 topics and lengths repeatedly. I think my loops are messed up. Any ideas on how to fix this and get the correct counts?

Hey BrilliantCoder23! :wave:

Looks like you’re working on some course stats, huh? That’s pretty cool! I’m curious, what kind of courses are these for?

I think I see what’s tripping you up with those loops. Have you considered using a different approach? Instead of nesting all those loops, maybe try grabbing the counts first and then displaying them? Something like:

@foreach($subjects as $subject)
    @php
        $unitCount = $subject->units->count();
        $topicCount = $subject->units->flatMap->topics->count();
        $totalLength = $subject->units->flatMap->topics->sum('length');
    @endphp
    
    <p class="content-summary">
        <span class="content-detail">{{ $unitCount }} unit(s)</span>
        <span class="content-detail">{{ $topicCount }} topic(s)</span>
        <span class="content-duration">{{ $totalLength }} minutes total</span>
    </p>
@endforeach

This way, you’re not repeating loops and it might be easier to manage. What do you think? Would this work for what you’re trying to do?

Also, I’m kinda interested in how you’re planning to use this info. Are you making some sort of course overview page?

hey mate, ur loop structure looks a bit off. try moving the topic loop inside the unit loop, not after it. Also, use ->count() instead of count(‘id’). for length, sum it up with $topic->sum(‘length’). that should fix the wonky numbers. good luck!

I’ve encountered similar issues with nested loops before. The problem lies in how you’re structuring your loops and accessing data. Here’s a suggestion to streamline your code:

<div class=\"content-wrapper\">
    @foreach($subjects as $subject)
    <p class=\"content-summary\">
        <span class=\"content-detail\">
            {{ $subject->units->count() }} units
        </span>
        <span class=\"content-detail\">
            {{ $subject->units->flatMap->topics->count() }} topics
        </span>
        <span class=\"content-duration\">
            {{ $subject->units->flatMap->topics->sum('length') }} minutes total
        </span>
    </p>
    @endforeach
</div>

This approach eliminates the need for nested loops by using Laravel’s collection methods. It should resolve the repetition issue and provide accurate counts for units, topics, and total duration per subject. Let me know if this helps or if you need further clarification.