简体   繁体   中英

Laravel multiple foreach in blade problem

I have segments. There are categories that segments have. Each segment consists of 3 categories and the middle segment is the main segment. How can I show them on the blade? 在此处输入图像描述

I write code like this.

    $seg = DB::table('segments')->get();
        $categories = DB::table('categories')
            ->join('category_translations', 'category_translations.category_id', 'categories.id')
            ->select('categories.id', 'category_translations.category_name')
            ->where('language_id', $lan->id)
            ->get();
        $seg_cat = DB::table('segment_categories')->get();


@foreach($seg as $segment)
    <section class="news-block">

        <div class="container">
            <div class="row">
                @foreach($seg_cat as $segment_categories)
                    @if($segment->id == $segment_categories->segment_id)
                        @foreach($categories as $cat)
                            @if($cat->id == $segment_categories->category_id)
                <div class="col-lg-3">
                    
                </div>
                <div class="col-lg-6">
                    </div>
                </div>
                <div class="col-lg-3">
                </div>
                            @endif
                        @endforeach
                    @endif
                @endforeach

And this gives me like this在此处输入图像描述

This situation is down to how you process and present the data which needs to be handled in the blade file.

The best option is to structure the code and foreach loops within the controller before passing the data to the front end to keep the logic more simple within the blade template.

Here is an example in a Json array which would work with two foreach loops and not make it more complicated within the blade file.

{
    "data": [
        {
            "segmentName1": "Foo",
            "categories": [
                {
                    "title": "Foo"
                },
                {
                    "title": "Boo"
                },
                {
                    "title": "Roo"
                }
            ]
        },
        {
            "segmentName2": "Boo",
            "categories": [
                {
                    "title": "Foo"
                },
                {
                    "title": "Boo"
                },
                {
                    "title": "Roo"
                }
            ]
        },
        {
            "segmentName3": "FooBoo",
            "categories": [
                {
                    "title": "Foo"
                },
                {
                    "title": "Boo"
                },
                {
                    "title": "Roo"
                }
            ]
        }
    ]
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM