首页 > 解决方案 > Laravel 嵌套循环

问题描述

我在尝试在刀片 @foreach 中执行嵌套循环时收到错误消息

这是我尝试过的,是的,我在上面使用了 2 key.. 有什么方法可以关联它吗?

这是我的控制器代码

$product = Product::where($where)->get();
        foreach($product as $key=>$value){
            if($value->SupplierID){
                $product[$key]['SupplierName'] = Company::find($value->SupplierID)->CompanyName;
            }else{
                $product[$key]['SupplierName'] = '-';
            }

            $product[$key]['ProductName'] = $value->getProdNameOne()->ProductName;

        }

        if(!empty(Auth::user())){
            $userid = Auth::user()->id;

            $companyfromusers = Companypersonstruct::where('user_id','=',$userid)->first();

            if(!empty($companyfromusers->user_id)){
                $companyrelationstruct = CompanyRelationStruct::where('FromCompanyID','=',$companyfromusers->CompanyID)->get();

                if(!empty($companyrelationstruct)){
                    foreach($companyrelationstruct as $key=>$crs){
                        $relatedcompany[] = Company::where('id','=',$crs->ToCompanyID)->get();
                    }
                } else {
                    $relatedcompany = '-';
                }
            } else {
                $relatedcompany = '-';
            }
        }

这是我的视图代码

@foreach ($product as $key=>$products)
        <tr>
        <th scope="row">{{ $products->ProductNumber }}</th>
        <td><a href="/detailproduct/{{ $products->id }}" target="_blank"> {{ $products->ProductName }}</a></td>
    @if(\Auth::user())
      @foreach($relatedcompany[$key] as $keychild=>$valchild)
        <td>{{ $valchild->CompanyName }}</td>
      @endforeach
        <td>{{ $products->UnitCustPrice }}</td>
    @endif
        </tr>
    @endforeach

这是我收到的错误:

未定义的偏移量:2

标签: phplaravelloopsnested-loops

解决方案


我相信有很多问题可以解决。

$product = Product::where($where)->get();
foreach ($product as $key => $value) {
    if ($value->SupplierID) {
        $product[$key]['SupplierName'] = Company::find($value->SupplierID)->CompanyName;
    } else {
        $product[$key]['SupplierName'] = '-';
    }
    $product[$key]['ProductName'] = $value->getProdNameOne()->ProductName;
}

$relatedcompany = false;
if (!empty(Auth::user())) {
    $userid = Auth::user()->id;

    $companyfromusers = Companypersonstruct::where('user_id', '=', $userid)->first();
    $relatedcompany = array();

    if (!empty($companyfromusers->user_id)) {
        $companyrelationstruct = CompanyRelationStruct::where('FromCompanyID', '=', $companyfromusers->CompanyID)->get();
        if (!empty($companyrelationstruct)) {
            foreach ($companyrelationstruct as $crs) {
                $relatedcompany[] = Company::find($crs->ToCompanyID);
            }
        }
    }
}

假设您已经正确传递了变量,这是可以使用$product$relatedcompany以上的视图代码。

@foreach ($product as $key=>$products)
  <tr>
    <th scope="row">{{ $products->ProductNumber }}</th>
    <td><a href="/detailproduct/{{ $products->id }}" target="_blank"> {{ $products->ProductName }}</a></td>
    @if ($relatedcompany !== false)
        @forelse ($relatedcompany as $company)
          @if ($company)
            <td>{{ $company->CompanyName }}</td>
          @endif
        @empty
          <td>-</td>
        @endforelse
      <td>{{ $products->UnitCustPrice }}</td>
    @endif
  </tr>
@endforeach

推荐阅读