我正在遍历公司和用户表,以便与各自的管理员和作者一起展示我的公司。
然而,我的过滤不正确,我在这里使用$ user->角色来检查比通过数据库和比较更快。我在管理专栏中收到了一些作者,反之亦然。
foreach($ company as $ company){?> ID>“>
<td><?= $company->name?></td>
<td><?= $company->city?></td>
<td><?= $company->group?></td>
<?php foreach ($company->users as $user) {?>
<?php
if($user->role == 'admin'){
?>
<td><?= $user->lastname?> <?= $user->firstname?> <?= $user->role?></td>
<?php
}
if($user->role == 'author'){?>
<td><?= $user->lastname?> <?= $user->firstname?> <?= $user->role?></td><?php
}
}
}
如何在我的第二个foreach中正确过滤,以便所有管理员都进入管理列和作者列中的作者?
由于
答案 0 :(得分:0)
通过添加foreach来修复它:
foreach ($companies as $company) {?>
<tr class='clickable-row' data-href="companies/edit/<?= $company->id?>">
<td><?= $company->name?></td>
<td><?= $company->city?></td>
<td><?= $company->group?></td>
<?php foreach ($company->users as $user) {?>
<?php
if($user->role == 'admin'){
?>
<td><?= $user->lastname?> <?= $user->firstname?></td>
<?php
}
}?>
<?php foreach ($company->users as $user) {?>
<?php
if($user->role == 'author'){?>
<td><?= $user->lastname?> <?= $user->firstname?> </td><?php
}
}
}