Tuesday, May 23, 2017

Nested Foreaches for Weekly Data

I have a list where customers can submit data weekly. I want to cycle through the weeks in a month (i.e. week 1, week 2...) and either show a value if it exists or show a select dropdown if not.

My problem is that I cant quite get the loops right. The below doesn't work as it outputs the data far too many times and i know the loops are in an incorrect order.

@if($customer->pay_schedule == 'weekly')
    @foreach($weeks as $week)
        @foreach($customer->payment as $payment)
            @if($week['week_start'] == $payment->pay_period_start & $week['week_end'] == $payment->pay_period_end)
                <div class="medium-3 columns">
                    Week 

                @foreach($statuses as $status)
                    @if($payment->pay_status == $status->id)
                        
                    @endif
                @endforeach
                </div>
            @else
                <div class="medium-3 columns">
                    Week 

                <select name="pay_status[]" class="form-control">
                    @foreach($statuses as $status)
                        <option value=""> </option>
                    @endforeach
                </select>
                </div>
            @endif
        @endforeach
    @endforeach
@endif

$customer->payment returns a collection of payments for that customer. So sometimes there is 4 weeks worth of data which is fine for the month.

But sometimes there is for example 3 weeks worth of data with week 4 missing which is the week where I would want a select to show so the data could be the entered.

Another example is there could be no data entered so a select would be needed for all 4 weeks of the month.

Hope that makes sense.



via Awilson089

Advertisement