在动态选项卡中检索数据

时间:2017-12-19 14:08:00

标签: php html twitter-bootstrap laravel

这里我试图点击任何标签来检索它自己的数据我做得很好而且它真的只检索一个原始数据?有什么帮助吗?

<div class="col-lg-12">
      <div class="tabs-container">
        <ul class="nav nav-tabs">
          @foreach($departments as $department)
          <li class=""><a data-toggle="tab" href="#tab-{{ $department->id }}" aria-expanded="false">{{ $department->name }}</a></li>
          @endforeach


        </ul>
        <div class="tab-content">
          @foreach($works as $work)
          <div id="tab-{{ $work->department_id }}" class="tab-pane">
            <div class="panel-body">
              <div class="row employees">

                <div class="col-md-3">
                  <div class="thumbnail">
                    <div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
                    <div class="caption">
                      <h4>{{ $work->address }}  </h4>
                      <p>{{ $work->body }}</p>
                    </div>
                  </div>
                  <a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
                  <button class="btn btn-danger demo4"> حذف
                  </button>
                </div>


              </div>
            </div>
          </div>
          @endforeach
        </div>
      </div>
    </div>
    <!--  -->
  </div>
如果有人有充分的理由说明为什么每个部门只给我一条记录,我不需要把它变成ajax吗?

1 个答案:

答案 0 :(得分:0)

你得到一个部门记录的原因是你的循环每个工作创建一个选项卡面板,例如,如果你有2个工程(1,2)属于一个部门(1),结果将是2个选项卡与相同的ID设置为1:

部门:

id     
-----
1

工作的:

id      department_id
---------------------
1       1
2       1

Html结果:

<div id="tab-1" class="tab-pane">
    ...
</div>
<div id="tab-1" class="tab-pane">
    ...
</div>

请注意,两个标签都具有相同的ID tab-1

<强>解决方案

group由department_id工作,如下所示:

 $works = Work::all()->groupBy('department_id');

然后在你看来:

<div class="tab-content">
    @foreach($works as $department_id => $department_works)
        <div id="tab-{{ $department_id }}" class="tab-pane">
            <div class="panel-body">
                <div class="row employees">
                    @foreach($department_works as $work)
                    <div class="col-md-3">
                        <div class="thumbnail">
                            <div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
                            <div class="caption">
                                <h4>{{ $work->address }}  </h4>
                                <p>{{ $work->body }}</p>
                            </div>
                        </div>
                        <a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
                        <button class="btn btn-danger demo4"> حذف </button>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    @endforeach
</div>

希望这有帮助