0

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

5
  • That's because $relatedcompany[2] doesn't exists, debug the data that you're sending in the controller Commented Dec 21, 2018 at 3:47
  • then, what i have to do? Commented Dec 21, 2018 at 4:02
  • I couldn't quite understand the logic of your code. It seems to me that there is no logical relation between your $products array and your $relatedcompany array. The $relatedcompany array seems to be only relevant to the login user. If so, why do you want to loop through the $relatedcompany in your $product array loop? Commented Dec 21, 2018 at 4:35
  • ya, theres no relation between products and relatedcompany. but i want to show relatedcompany for each product.. Commented Dec 21, 2018 at 5:46
  • relatedcompany only have relation with user... Commented Dec 21, 2018 at 5:46

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

then how to avoid using key?
What do you mean by "avoid using key"? You current code suggested you have 2 separated arrays: $product and $relatedcompany. And there is nothing co-relating the 2 except for the $key. If this is not the case, there is a logically error in your code. And if that's so, you have to provide more information or code for us to understand your problem.
code above give me Property [CompanyName] does not exist on this collection instance.
Updated. Please try again. (BTW, you should be able to fix this by understanding what is a collection)
0

You should try this:

@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())
      @if(isset($relatedcompany[$key]))
        @foreach($relatedcompany[$key] as $keychild=>$valchild)
          <td>{{ $valchild->CompanyName }}</td>
        @endforeach
      @endif
        <td>{{ $products->UnitCustPrice }}</td>
    @endif
        </tr>
    @endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.