php - 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
解决方案
我相信有很多问题可以解决。
$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
推荐阅读
- django - How to post one html form data into two models in django?
- sql - SQL - Group and Aggregation to calculate ratio with respect to total
- android - Flutter - 在 Android Studio 中构建 apk 时出错
- javascript - 我无法建立数据库连接,它会显示错误
- google-cloud-platform - Stackdriver 是否提供 C++ 客户端库?
- audio - 我们如何解决 HTTP 请求错误?错误 429,请求过多
- docker-compose - 在 Docker 镜像上安装 Docker 和 Docker Compose
- numpy - ValueError: could not convert string to float:'\ufeff2',如何将数组中不同类型的数据转换为float类型?
- kubernetes - 如何在 Windows 10 上迁移 kubeconfig
- php - 使用两个分隔符分解字符串并遍历结果