为什么嵌套循环在 laravel 中不起作用
why nested loop not working in laravel
这是我的看法,我不知道为什么我的嵌套循环不起作用,请帮助我
<ul>
@foreach($users as $m)
<li> {{$m->namecategory}}
<ul>
@foreach($m->namesubcategory as $nam)
<li>{{$nam->namesubcategory}}
<ul>
@foreach($nam->namesubling as $lan)
<li>{{$lan->namesubling}}</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
它的抛出错误是这样的
为 foreach() 提供的参数无效(视图
这里是使用 dd($users)
的输出
array:3 [▼
0 => {#209 ▼
+"idcategory": 1
+"namecategory": "mobile and assces"
+"created_at": null
+"updated_at": null
+"idsubcategory": 1
+"namesubcategory": "mobile"
+"idcategory_mastercategory": 1
+"idsubling": 1
+"namesubling": "iphone"
+"idsubcategory_subcategory": 1
}
1 => {#211 ▼
+"idcategory": 1
+"namecategory": "mobile and assces"
+"created_at": null
+"updated_at": null
+"idsubcategory": 2
+"namesubcategory": "mobile cover"
+"idcategory_mastercategory": 1
+"idsubling": 2
+"namesubling": "nexu cover"
+"idsubcategory_subcategory": 2
}
2 => {#212 ▼
+"idcategory": 2
+"namecategory": "elect"
+"created_at": null
+"updated_at": null
+"idsubcategory": 3
+"namesubcategory": "lap"
+"idcategory_mastercategory": 2
+"idsubling": 3
+"namesubling": "hp"
+"idsubcategory_subcategory": 3
}
]
我正在努力实现这个目标
-mobile and accessory (mastercategory)
1.mobile(subcategory)
1.iphone(subling)
2.mobile cover(subcategory)
1.iphone cover(subling)
-electronic(mastercategory)
1.laptop(subcategory)
1.hp(subling)
根据您的 var_dump,$m->namesubcategory - 不是数组。
$m->namesubcategory, $nam->namesubling 也不行。
你不能对他们使用 "foreach"。
您必须根据 collection 创建另一个数组。
然后你就可以得到你需要的任何东西。
$result = [];
foreach ($users as $u) {
$result[ $u-> namecategory ] = $result[ $u-> namecategory ] ?? [];
$result[ $u-> namecategory ][$namesubcategory] = $result[ $u-> namecategory ][$namesubcategory] ?? [];
$result[ $u-> namecategory ][$namesubcategory][] = $u-> namesubling;
}
结果你会得到一个类似
的数组
result [mobile and assces]
[mobile]
[iphone]
[mobile cover]
[nexu cover]
然后你就可以"foreach"这个数组了。
请注意,语法是“$a = $b['xx'] ?? [];” - PHP7
这是我的看法,我不知道为什么我的嵌套循环不起作用,请帮助我
<ul>
@foreach($users as $m)
<li> {{$m->namecategory}}
<ul>
@foreach($m->namesubcategory as $nam)
<li>{{$nam->namesubcategory}}
<ul>
@foreach($nam->namesubling as $lan)
<li>{{$lan->namesubling}}</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
它的抛出错误是这样的
为 foreach() 提供的参数无效(视图
这里是使用 dd($users)
的输出array:3 [▼
0 => {#209 ▼
+"idcategory": 1
+"namecategory": "mobile and assces"
+"created_at": null
+"updated_at": null
+"idsubcategory": 1
+"namesubcategory": "mobile"
+"idcategory_mastercategory": 1
+"idsubling": 1
+"namesubling": "iphone"
+"idsubcategory_subcategory": 1
}
1 => {#211 ▼
+"idcategory": 1
+"namecategory": "mobile and assces"
+"created_at": null
+"updated_at": null
+"idsubcategory": 2
+"namesubcategory": "mobile cover"
+"idcategory_mastercategory": 1
+"idsubling": 2
+"namesubling": "nexu cover"
+"idsubcategory_subcategory": 2
}
2 => {#212 ▼
+"idcategory": 2
+"namecategory": "elect"
+"created_at": null
+"updated_at": null
+"idsubcategory": 3
+"namesubcategory": "lap"
+"idcategory_mastercategory": 2
+"idsubling": 3
+"namesubling": "hp"
+"idsubcategory_subcategory": 3
}
]
我正在努力实现这个目标
-mobile and accessory (mastercategory)
1.mobile(subcategory)
1.iphone(subling)
2.mobile cover(subcategory)
1.iphone cover(subling)
-electronic(mastercategory)
1.laptop(subcategory)
1.hp(subling)
根据您的 var_dump,$m->namesubcategory - 不是数组。 $m->namesubcategory, $nam->namesubling 也不行。 你不能对他们使用 "foreach"。
您必须根据 collection 创建另一个数组。 然后你就可以得到你需要的任何东西。
$result = [];
foreach ($users as $u) {
$result[ $u-> namecategory ] = $result[ $u-> namecategory ] ?? [];
$result[ $u-> namecategory ][$namesubcategory] = $result[ $u-> namecategory ][$namesubcategory] ?? [];
$result[ $u-> namecategory ][$namesubcategory][] = $u-> namesubling;
}
结果你会得到一个类似
的数组result [mobile and assces]
[mobile]
[iphone]
[mobile cover]
[nexu cover]
然后你就可以"foreach"这个数组了。
请注意,语法是“$a = $b['xx'] ?? [];” - PHP7