通过Blade foreach访问集合

时间:2018-08-02 11:14:14

标签: laravel

enter image description here

@foreach ($despatchItems as $despatchItem)
            {{dd($despatchItem)}}
             <td><input type="text" class="form-control" id="inputQty"  name="qty" value="{{$despatchItem->qty}}"></td>
@endforeach

我需要获取这些数组的数量(在这种情况下,这里有3个Collections)


The Under为我工作

@foreach ($despatchItems as $collections)
    @foreach($collections as $collection)
        @foreach($collection as $despatchItem)
            <td><input type="text" class="form-control" id="inputQty" name="qty" value="{{$despatchItem->qty}}"></td>
        @endforeach
    @endforeach
@endforeach

我的控制器方法如下

public function create($jobId)
{


  $job = Job::find($jobId);

  $despatches = $job->despatches;


  foreach ($despatches as $despatch) {
    $despatchItems['despatchItems'][] = $despatch->despatch_items;
  }

  return view('employees.invoices.create',compact('despatchItems','job'));
}

1 个答案:

答案 0 :(得分:2)

您有DespatchItem的集合

@foreach ($despatchItems as $despatchItem)
   {{dd($despatchItem->qty)}}
 @endforeach

如果需要数量数组,请尝试以下操作:

$despatchItems->map(function ($despatchItem) {
    return $despatchItems->qty;
})