简体   繁体   中英

How can I add a blank row at the end of this nested foreach? - Laravel Blade

I have a table of game schedules. In my controller I am doing this;

    $schedule = Schedule::where('season', '=', $season->id)
    ->where('date', '>', Carbon::now()->subDays(1))->get();

    foreach ($schedule as $item) {
        $date = new Carbon($item['date']);
        $months[$date->format("F Y")][] = $item;
    }

This allows me to then do this in my blade view;

    @foreach ($months as $month => $items)
        <tr style="background-color: rgba(101, 101, 101, 0.4);">
           <td colspan="5">
             <strong>{{ $month }}</strong>
            </td>
        </tr>
        @foreach ($items as $item)
          <tr>
            <td width="20%">{{ date('D, M j', strtotime ($item['date'])) }}/td>
            <td width="20%">{{ $item['time'] }}</td>
            <td width="25%"><a href="{{ url('team', $item['home_team_id']) }}">{{ $item['home_team'] }}</a></td>
            <td width="25%"><a href="{{ url('team', $item['away_team_id']) }}">{{ $item['away_team'] }}</a></td>
            <td width="10%"><a href="gamesheet/{{ $item['id'] }}" target="_blank"><i class="fa fa-print" aria-hidden="true"></i></a></td>
          </tr>
         @endforeach
      @endforeach

So far so good in that this grabs the month and creates a distinct row with the month as a heading.

Now, I would like to try to do the same with each week. To get something like this;

November 2016
Mon Nov 7 7:00PM Team A   Team B
Mon Nov 7 8:00PM Team C   Team D
<empty row here>
Mon Nov 14 7:00PM Team D Team A
Mon Nov 14 8:00PM Team B Team C
<empty row here>

I have tried defining a week array in my controller like this;

$weeks[$date->format("M j")][] = $item;

But it needs to also end the loop when the month changes. Any ideas? Should I be trying to do a further loop in the controller?

Thanks!

You could achieve this using an if statement inside the foreach loop.

You just want to add this at the end of your nested foreach loop:

@if ($item == end($items))
  <tr></tr>
@endif

Which will check if the current $item is the last item in $items .

Something like this should get you most of the way there:

@foreach ($items as $item)
  <tr>
    <td width="20%">{{ date('D, M j', strtotime ($item['date'])) }}/td>
    <td width="20%">{{ $item['time'] }}</td>
    <td width="25%"><a href="{{ url('team', $item['home_team_id']) }}">{{ $item['home_team'] }}</a></td>
    <td width="25%"><a href="{{ url('team', $item['away_team_id']) }}">{{ $item['away_team'] }}</a></td>
    <td width="10%"><a href="gamesheet/{{ $item['id'] }}" target="_blank"><i class="fa fa-print" aria-hidden="true"></i></a></td>
  </tr>
  @if ($item == end($items))
  <tr></tr>
  @endif
@endforeach

Although looking at your current foreach loops you would likely end up with this:

November 2016
Mon Nov 7 7:00PM Team A   Team B
Mon Nov 7 8:00PM Team C   Team D
Mon Nov 14 7:00PM Team D Team A
Mon Nov 14 8:00PM Team B Team C
<empty row here>

So if you can just break the foreach loop down further by date then you could achieve what you're after with this method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM