在laravel v5.3中显示表格

时间:2017-06-17 00:49:48

标签: php mysql laravel laravel-5.3

我只是想显示表格中的数据,您可以在下面的图片中看到:
project_id指项目表中的项目名称
activity_id是指活动表中的活动名称
image show table fields

我要显示的结果与此图片相同:
result i want to show at web page

这是我的代码

<table id="list-Dopeople-report" class="table table-bordered responsive">
          <tr> 
          <th>project</th>           
             @foreach($examples as $key=>$value)
                 <th>{{activityIdToActivityNameConvert($value->activity->id)}}</th>
             @endforeach
             </tr>            
             @foreach($pros as $key=>$value)
             <tr>
                 <th>{{projectIdToProjectNameConvert($value->project->id)}}</th> 
            </tr>
             @endforeach
     </table>

但结果变得如下图所示:
wrong result

任何帮助都是适当的

1 个答案:

答案 0 :(得分:0)

这里有几点需要考虑:

  1. 为了获得最佳做法,请添加&#39; thead&#39;和&#39; tbody&#39;标签
  2. 在你的第二个foreach中,你继续使用&#39; th,用于表格标题。使用&#39;&#39;代替。
  3. 主要问题是你只是迭代了&#39; -e元素,这将继续添加行。你不会迭代'td&#39;元素,将填充列

    <table id="list-Dopeople-report" class="table table-bordered responsive">
        <thead>      
            <tr> 
                <th>project</th>           
                @foreach($examples as $key=>$value)
                    <th>{{activityIdToActivityNameConvert($value->activity->id)}}</th>
                @endforeach
            </tr>
        </thead>
        <tbody> 
        <!-- here you iterate over the rows, which is ok -->           
        @foreach($pros as $key=>$value)
            <tr>
                <td>{{projectIdToProjectNameConvert($value->project->id)}}</td> 
                <!-- you should also iterate over the columns -->
                @foreach($project->activities as $activity)
                    <td>{{$activity->time}}</td>
                @endforeach
            </tr>
         @endforeach
         </tbody>
     </table>