根据条件显示数据

时间:2017-01-31 13:07:46

标签: laravel laravel-5.2 laravel-blade

我允许上传文件。在上传时,选择了一个月。现在在我看来,我传递了与上传相关的所有数据,这些数据在本月是唯一的。从本质上讲,我的观点最终会出现类似这样的内容

entityManager.createQuery("select p.name,d.deptname " + " from Person a,Department d " + "where a.id_dep =d.id_dep ").getResultList();

现在我希望显示标签中我所拥有的任何月份的数据。为了演示目的,我已经删除了大部分月份

array:2 [▼
  0 => {#235 ▼
    +"id": 11
    +"upload_month": "Janaury"
    +"MAX(created_at)": "2017-01-30 13:25:59"
  }
  1 => {#236 ▼
    +"id": 10
    +"upload_month": "April"
    +"MAX(created_at)": "2017-01-30 13:22:39"
  }
]

使用传递给视图的数据(包含数据相关的月份),如何在适当的选项卡中显示它?

任何建议表示赞赏

由于

1 个答案:

答案 0 :(得分:1)

最简单的方法可能是利用控制器中的内置groupBy方法:

// instead of
// return $someDataObject;
return $someDataObject->groupBy('upload_month');

然后在视图中,您可以预约月份键:

<ul class="nav nav-tabs">
    <li class="active"><a href="#January" data-toggle="tab">JAN</a></li>
    <li><a href="#February" data-toggle="tab">FEB</a></li>
    <li><a href="#March" data-toggle="tab">MAR</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane active" id="January">
        <h3>Jan Data</h3>
        @foreach($collectionOfData['January'] as $monthData)
            {{ $monthData->id }}
        @endforeach
    </div>
    <div class="tab-pane" id="February">
        <h3>Feb Data</h3>
        @foreach($collectionOfData['February'] as $monthData)
            {{ $monthData->id }}
        @endforeach
    </div>
    <div class="tab-pane" id="March">
        <h3>Mar data</h3>
        @foreach($collectionOfData['March'] as $monthData)
            {{ $monthData->id }}
        @endforeach
    </div>
    .....
</div>

确切的实施当然取决于你的代码,但这应该让你开始。

如果您在后端进行了一些排序(以便月份的顺序正确),您甚至可以更简化代码,例如:

return $someDataObject->groupBy('upload_month')->sortBy('someDateRepresentationOfTheMonthTheDataRepresents');

在视图中:

<ul class="nav nav-tabs">
    @foreach($collectionOfData as $monthName => $monthData)
        {{-- This uses the $loop variable which is only available in Laravel 5.3 and above --}}
        <li class="<?php if($loop->first) { echo 'active'; } ?>"><a href="#{{ $monthName }}" data-toggle="tab"><?php strtoupper(date('M', strtotime($monthName))); ?></a></li>
    @endforeach
</ul>

<div class="tab-content">
    @foreach($collectionOfData as $monthName => $monthData)
        {{-- This uses the $loop variable which is only available in Laravel 5.3 and above --}}
        <div class="tab-pane <?php if($loop->first) { echo 'active'; } ?>" id="{{ $monthName }}">
            <h3><?php date('M', strtotime($monthName)); ?> Data</h3>
            @foreach($monthData as $monthDataPieces)
                {{ $monthDataPieces->id }}
            @endforeach
        </div>
    @endforeach
</div>