Search code examples
phplaravelloopsnested-loops

Laravel nested loop


I'm receiving an error trying to do a nested loop in a blade @foreach

This is what I've tried, yeam im using 2 key on it.. is there any way to relation it?

here is my controller code

$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 = '-';
            }
        }

and this is my view code

@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

This is the error that I'm receiving:

Undefined offset: 2


Solution

  • I believe there are multiple problems that can be addressed.

    $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);
                }
            }
        }
    }
    

    Supposed you have passed the variables correctly, this is the view code that can work with $product and $relatedcompany above.

    @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