每周数据的嵌套Foreach

时间:2017-05-23 21:53:38

标签: laravel laravel-5 eloquent

我有一个列表,客户可以每周提交数据。我希望在一个月内(即第1周,第2周......)循环显示这几周,如果存在则显示一个值,否则显示一个选择下拉列表。

我的问题是我不能让循环正确。以下不起作用,因为它输出的数据太多次了,我知道循环的顺序不正确。

@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 {{ $loop->iteration }}

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

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

$customer->payment会返回该客户的付款集合。所以有时候有4个星期的数据,这个月很好。

但有时会有第3周缺失的3周数据,这是我希望选择显示的那一周,因此可以输入数据。

另一个例子是可能没有输入数据,因此一个月的所有4周都需要选择。

$周数据

array:5 [▼
  0 => array:2 [▼
    "week_start" => Carbon {#239 ▼
      +"date": "2017-05-01 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
    "week_end" => Carbon {#255 ▼
      +"date": "2017-05-08 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
  ]
  1 => array:2 [▼
    "week_start" => Carbon {#256 ▼
      +"date": "2017-05-08 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
    "week_end" => Carbon {#254 ▼
      +"date": "2017-05-15 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
  ]
  2 => array:2 [▼
    "week_start" => Carbon {#247 ▼
      +"date": "2017-05-15 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
    "week_end" => Carbon {#295 ▼
      +"date": "2017-05-22 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
  ]
  3 => array:2 [▼
    "week_start" => Carbon {#271 ▼
      +"date": "2017-05-22 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
    "week_end" => Carbon {#293 ▼
      +"date": "2017-05-29 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
  ]
  4 => array:2 [▼
    "week_start" => Carbon {#291 ▼
      +"date": "2017-05-29 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
    "week_end" => Carbon {#290 ▼
      +"date": "2017-06-05 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/London"
    }
  ]
]

$ customer-&gt;付款数据

[
    {"id":114,"user_id":1,"customer_id":8,"pay_status":1,"pay_schedule":"weekly","types_supplied":"Pies","pay_period_start":"2017-05-01 00:00:00","pay_period_end":"2017-05-08 00:00:00","created_at":"2017-05-21 17:35:23","updated_at":"2017-05-21 17:35:23"}, 
    {"id":115,"user_id":1,"customer_id":8,"pay_status":1,"pay_schedule":"weekly","types_supplied":"Pies","pay_period_start":"2017-05-08 00:00:00","pay_period_end":"2017-05-15 00:00:00","created_at":"2017-05-21 17:35:23","updated_at":"2017-05-21 17:35:23"},
    {"id":116,"user_id":1,"customer_id":8,"pay_status":1,"pay_schedule":"weekly","types_supplied":"Pies","pay_period_start":"2017-05-15 00:00:00","pay_period_end":"2017-05-22 00:00:00","created_at":"2017-05-21 17:35:23","updated_at":"2017-05-21 17:35:23"},
    {"id":117,"user_id":1,"customer_id":8,"pay_status":1,"pay_schedule":"weekly","types_supplied":"Pies","pay_period_start":"2017-05-22 00:00:00","pay_period_end":"2017-05-29 00:00:00","created_at":"2017-05-21 17:35:23","updated_at":"2017-05-21 17:35:23"}
]

希望这是有道理的。

1 个答案:

答案 0 :(得分:1)

集合可用于此..

{{ $payments = collect($customer->payments) }}
@foreach($weeks as $week)
    {{ $filtered = $payments->where('pay_period_start ', $week['week_start'])->where('pay_period_end',$week['week_end']) }}
    @if(count($filtered) > 0)
        <div class="medium-3 columns">
            Week {{ $loop->iteration }}
            // also in this section .. you should have a relation from payment to status .. like payment belongsTo('App\status','pay_status');
            {{ $filtered->first()->status }}
        </div>
    @else
        // .. do your select here
    @endif
@endforeach