mardi 27 décembre 2016

Laravel: How can I set dynamic table headers from a nested foreach loop?

I have this markup in my blade template:

<th>Name</th>
    @foreach($camp->athletes as $athlete)
        @foreach($athlete->kickoffs as $key=>$kickoff)
            <th>D#</th>
            <th>H#</th>
        @endforeach
    @endforeach
<th>Avg. Distance</th>
<th>Avg. Hang Time</th>
<th>Score</th>

Which works fantastic when I only have a single athlete. But, when I start adding more, it's obvious that I am going to repeat the <th> elements for each athlete. Something like this is what I am getting:

<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>

<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>

I totally understand why I am getting duplicates like that - I'm not sure how to avoid that though. It's like I need to do this:

...
<th>Name</th>

        @foreach($athlete->kickoffs as $key=>$kickoff)
            <th>D#</th>
            <th>H#</th>
        @endforeach

<th>Avg. Distance</th>
...

That way I only get this output:

<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>

But I can't access the $athlete variable directly like that.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hqZOKa
via IFTTT

Aucun commentaire:

Enregistrer un commentaire