Foreach在Laravel中使用不同的查询从一个数据库表中调用值

时间:2018-07-17 23:33:45

标签: php laravel

我想从一个数据库表中检索值,但根据不同的id_produk(外​​键)值将其分成两个相邻的表。

这是Controller中的代码:

public function index()
{
  $item_ict  = Item::where('id_produk', '1');
  $item_cm   = Item::where('id_produk', '2');

  return view('item/index', compact('item_ict', 'item_cm'));
}

然后,我在索引中调用$ item_ict和$ item_cm

<table class="table">
  <thead>
    <tr>
      <th>ICT</th>
      <th>CM</th>
    </tr>
  </thead>
  <tbody>
    @foreach ($item_ict as $itemict)
    @foreach ($item_cm as $itemcm)
    <tr>
      <td>{{ $itemict -> nama_item }}</td>
      <td>{{ $itemcm -> nama_item }}</td>
    </tr>
    @endforeach
    @endforeach
  </tbody>
</table>

当我写那个foreach时这是正确的方法吗?没有任何错误,但没有值退出。如何解决?

或者我正在考虑使用索引页中的查询来调用它,但是我不知道如何。可能吗?怎么样?

1 个答案:

答案 0 :(得分:0)

将项目组合成一个数组,这样您只需要执行一次循环即可。

另外,使用Type inference failed: Cannot infer type parameter T in fun <T : Comparable<T#1 (type parameter of kotlin.ranges.coerceAtLeast)>> T#1.coerceAtLeast(minimumValue: T#1): T#1 None of the following substitutions receiver: Any? arguments: (Any?) receiver: Comparable<T#2 (type parameter of com.nelsonirrigation.twig.plans.extensions.max)> arguments: (Comparable<T#2>) receiver: T#2 arguments: (T#2) receiver: Comparable<Comparable<T#2>> arguments: (Comparable<Comparable<T#2>>) can be applied to receiver: Comparable<T#2> arguments: (T#2) 实际获得物品。就目前而言,您的代码仍然只是查询生成器。

->get()