0

我有 db 表LovColumnComments,它包含:

----------------------------------------------------
| id | view          | column       | description  |
----------------------------------------------------
| 1  | my_view       | object_id    | Object ID    |
| 2  | my_view       | description  | Description  |
----------------------------------------------------

我有另一个DataTable包含实际数据的表。

-------------------------------------
| id |  object_id    | description  |
-------------------------------------
| 1  |  OBJ_01       | Object 01    |
| 2  |  OBJ_02       | Object 02    |
-------------------------------------

这两个表都作为集合传递给刀片视图。

我现在拥有的刀片模板是:

<table>
  <thead>
     <tr>
        @foreach ($columns as $column)
          <th>{{$column->description}}</th>
        @endforeach
     </tr>
   </thead>
   <tbody>
      @foreach ($data as $datum)
        <tr>
          <td>{{$datum['object_id']}}</td>
          <td>{{$datum['description']}}</td>
        </tr>
      @endforeach
   </tbody>
</table>

我想知道我是否可以访问$data集合而不是使用$datum['object_id']and $datum['description']。这是因为,DataTable可以是任何数据库表,因此字段名称可以不同,但​​总是会有两列。

因此,$data集合总是有两列,但列名可以不同。$columns我需要一种更动态的方式来从集合中获取列名。

类似的东西,<td>{{$columns[0]}}</td><td>{{$columns[1]}}</td>

仅供参考,columnof$columns包含实际名称(即object_iddescription

谢谢

4

2 回答 2

0
@foreach ($data as $datum)
  <tr>
    @foreach ($columns as $column)
      <td>{{$datum[$column->column]}}</td>
    @endforeach
  </tr>
@endforeach
于 2017-08-01T23:50:44.577 回答
0

So here is my example of how I would do this in my blade view:

@if(count($data) > 0)
    @php
        $keys = array_keys($data[0]);
    @endphp
    @foreach($data as $d)
        @foreach($keys as $key)
            <!-- This is the dynamic value that works with any array -->
            {{$d[$key]}}
        @endforeach
    @endforeach
@endif

Let me know if you have any questions, I will happily try and help!

于 2017-08-01T23:42:07.250 回答