drgwsx8405 2017-09-08 15:51
浏览 66
已采纳

Laravel 5.4带有分组数据集的嵌套循环

"Filters" table looks like following:

id |  group   |       label       |        value        
----+----------+-------------------+----------------

 12 | deadline | 1 to 3 days       | 1 AND 3            
 14 | price    | Any               | 0                  
 15 | price    | Less than 1000    | 0 AND 1000         
 16 | price    | 1000 to 10000     | 1000 AND 10000     
 22 | location | Any               | 0                  
 23 | location | New York          | newyork            
 24 | location | California        | california         
 25 | location | Alabama           | alabama 

I need to show chunk of "label" under the "group".

My controller:

public function index(){
        $filters = Filters::all();
        return view('frontend.request', compact('filters'));
    }

My view has:

<?php foreach ($filters as $filter) { ?>
                    <div class="panel panel-default">
                        <b>{{ $filter->group }}</b>
                        <div id="collapseOne1" class="panel-collapse collapse in">
                            <div class="panel-body">
                                <div class="checkbox">
                                    <label><input type="checkbox" name="{{ $filter->group }}" value="{{ $filter->any }}">{{ $filter->label }}</label>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>

I want to show all labels under the group within one box and so on. The above codes create different boxes for each label which is not right.

I believe, its got to be nested loop but I'm having difficult writing it on Laravel. Any idea how it should be?

Note: This thread gives partial answer but we don't know chunk size in my case: Laravel 5.1 - Nested Loop - Blade Templating

  • 写回答

1条回答 默认 最新

  • dongwu4834 2017-09-08 16:14
    关注

    You could use Laravel's groupBy() collection method;

    https://laravel.com/docs/5.4/collections#method-groupby

    Controller:

    public function index(){
        $filters = Filters::all();
        $groups = $filters->groupBy('group');
        return view('frontend.request', compact('groups'));
    }
    

    View:

    @foreach ($groups as $group => $filters)
        <div class="panel panel-default">
            <b>{{ $group }}</b>
            @foreach($filters as $filter)
                <div id="collapseOne1" class="panel-collapse collapse in">
                    <div class="panel-body">
                        <div class="checkbox">
                            <label><input type="checkbox" name="{{ $filter->group }}" value="{{ $filter->any }}">{{ $filter->label }}</label>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @endforech
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持