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
$productsarray and your$relatedcompanyarray. The$relatedcompanyarray seems to be only relevant to the login user. If so, why do you want to loop through the$relatedcompanyin your$productarray loop?productsandrelatedcompany. but i want to showrelatedcompanyfor eachproduct..relatedcompanyonly have relation withuser...