通过一个表循环两次

时间:2017-08-31 06:14:25

标签: laravel laravel-5

我正在尝试创建一个包含表格的页面。该表有一个服务,然后是服务下的项目,因此该表应该如下所示

<table class="table table-striped table-bordered table-condensed pricing_table">
    <tr>
        <td></td>
        <td>
            Short
        </td>
        <td>
            Medium
        </td>
        <td>
            Long
        </td>
    </tr>

    <tr>
        <td>
            Cut & Blow Dry
        </td>
        <td>
            R170
        </td>
        <td>
            R190
        </td>
        <td>
            R220
        </td>
    </tr>

    <tr>
        <td>
            Blow Dry
        </td>
        <td>
            R120
        </td>
        <td>
            R170
        </td>
        <td>
            R190
        </td>
    </tr>

    <tr>
        <td>
            Girls under 18
        </td>
        <td>
            R130
        </td>
        <td></td>
        <td></td>
    </tr>

    <tr>
        <td>
            Pensioners
        </td>
        <td>
            R130
        </td>
        <td></td>
        <td></td>
    </tr>

    <tr>
        <td>
            Gents Cut
        </td>
        <td>
            R120
        </td>
        <td></td>
        <td></td>
    </tr>

    <tr>
        <td>
            Boys Cut
        </td>
        <td>
            R90
        </td>
        <td></td>
        <td></td>
    </tr>

    <tr>
        <td>
            Upstyles - Trail
        </td>
        <td>
            R270
        </td>
        <td></td>
        <td></td>
    </tr>

    <tr>
        <td>
            Upstyles
        </td>
        <td>
            R350
        </td>
        <td></td>
        <td></td>
    </tr>
</table>

但我得到的是这个

<table class="table table-striped table-bordered table-condensed pricing_table">
    <tbody>
        <tr>
            <td> Cutting & Styling </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Cut & Blow Dry </td>
            <td> 150 </td>
            <td> 170 </td>
            <td> 190 </td>
        </tr>

        <tr>
            <td> Cutting & Styling </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Blow Dry </td>
            <td> 100 </td>
            <td> 120 </td>
            <td> 140 </td>
        </tr>

        <tr>
            <td> Cutting & Styling </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Girls under 18 </td>
            <td> 120 </td>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td> Cutting & Styling </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Pensioners (Ladies) </td>
            <td> 70 </td>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Color </td>
            <td> 150 </td>
            <td> 200 </td>
            <td> 250 </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Half Head Foils </td>
            <td> 200 </td>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Full Head Foils </td>
            <td> 300 </td>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Per Foil </td>
            <td> 10 </td>
            <td> 15 </td>
            <td> 20 </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Brazilian </td>
            <td> 700 </td>
            <td> 800 </td>
            <td> 900 </td>
        </tr>

        <tr>
            <td> Chemical Service </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Perm </td>
            <td> 150 </td>
            <td> 170 </td>
            <td> 190 </td>
        </tr>

        <tr>
            <td> Treatment </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Salon Treatment </td>
            <td> 100 </td>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td> Treatment </td>
            <td> Short </td>
            <td> Medium </td>
            <td> Long </td>
        </tr>

        <tr>
            <td> Olaplex - Stand Alon </td>
            <td> 180 </td>
            <td> </td>
            <td> </td>
        </tr>
    </tbody>
</table>

服务始终循环显示。我只希望服务一次循环,并在各自的服务下显示项目。

我的pricing.blade.php

<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 about_content_area">
        <h1>pricing</h1>

        <div class="col-lg-5 col-lg-offset-3 col-md-5 col-md-offset-3 pricing_wrapper">
            <table class="table table-striped table-bordered table-condensed pricing_table">
                @foreach($services_options as $services)
                    @foreach($services->service as $service)
                        <tr>
                            <td>
                                {!! $service->title !!}
                            </td>
                            <td>
                                Short
                            </td>
                            <td>
                                Medium
                            </td>
                            <td>
                                Long
                            </td>
                        </tr>
                    @endforeach

                        <tr>
                            <td>
                                {!! $services->title !!}
                            </td>
                            <td>
                                {!! $services->short !!}
                            </td>
                            <td>
                                {!! $services->medium !!}
                            </td>
                            <td>
                                {!! $services->long !!}
                            </td>
                        </tr>
                @endforeach
            </table>
        </div>
    </div>
</div>

我的控制器

public function content($id)
    {
        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();

        $menu = Menu::where('id', $id)->firstOrFail();

        $layout = $menu->type;

        $gallery_category = Gcategory::all();

        $services_options = Price::all();

        return view('open::public/'.$layout, compact('menus_child', 'menu', 'gallery_category', 'services_options'));
    }

1 个答案:

答案 0 :(得分:0)

在每个服务的循环中,你有一个表头部分(短,中,长部分)的循环,这就是它在每个服务行上面输出的原因。

您只需将pricing.blade.php更新为以下

即可
<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 about_content_area">
        <h1>pricing</h1>

        <div class="col-lg-5 col-lg-offset-3 col-md-5 col-md-offset-3 pricing_wrapper">
            <table class="table table-striped table-bordered table-condensed pricing_table">
                <tr>
                    <td></td>
                    <td>
                        Short
                    </td>
                    <td>
                        Medium
                    </td>
                    <td>
                        Long
                    </td>
                </tr>
                @foreach($services_options as $services)
                    <tr>
                        <td>
                            {!! $services->title !!}
                        </td>
                        <td>
                            {!! $services->short !!}
                        </td>
                        <td>
                            {!! $services->medium !!}
                        </td>
                        <td>
                            {!! $services->long !!}
                        </td>
                    </tr>
                @endforeach
            </table>
        </div>
    </div>
</div>